• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTgx

Related Code
Generic Entity Framework 4.0 Base Repository
ForEach Extension method for IEnumerable<T>
Invoke Extension Method
Use of Extension methods to hide "infrastructure code"
Unity Service Locator for ASP.NET MVC 3.0 Beta 1
Null Dot "Operator" Extension Method
Generic Entity Framework 4.0 Base Repository with Paging
Generic linq-to-sql repository
WCF RIA Services Unity 2.0 DomainService Factory
A Default Entity Framework 4.0 ObjectContext Factory
UnitOfWork Action filter for ASP.NET MVC and nHibernate
Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor
Email validation with Regular Expression
Reflection dynamically get private fields or properties
Url Validation with Regular Expression
Method to hash passwords
Number of sealed / unsealed types in the framework
Extension to the HttpClient

Custom C# exception class

Minimum code required to create a custom exception in C#

0
637 0 0 0 0 0

A simple custom exception class.

using System;
using System.Runtime.Serialization;

[Serializable]
public class CustomException : Exception {

  public CustomException() { }

  public CustomException(String message) : base(message) { }

  public CustomException(String message, Exception inner) : base(message, inner) { }

  protected CustomException(SerializationInfo info, StreamingContext context)
    : base(info, context) { }

}

The exception class can also carry some additional data:

using System;
using System.Runtime.Serialization;

[Serializable]
public class CustomException : Exception {

  readonly Int32 data;

  public CustomException() { }

  public CustomException(Int32 data) : base(FormatMessage(data)) {
    this.data = data;
  }

  public CustomException(String message) : base(message) { }

  public CustomException(Int32 data, Exception inner) : base(FormatMessage(data), inner) {
    this.data = data;
  }

  public CustomException(String message, Exception inner) : base(message, inner) { }

  protected CustomException(SerializationInfo info, StreamingContext context) : base(info, context) { }

  public override void GetObjectData(SerializationInfo info, StreamingContext context) {
    if (info == null)
      throw new ArgumentNullException("info");
    info.AddValue("data", this.data);
    base.GetObjectData(info, context);
  }

  public Int32 Data { get { return this.data; } }

  static String FormatMessage(Int32 data) {
    return String.Format("Custom exception with data {0}.", data);
  }

}

Consider creating a hierarchy of custom exceptions.

Also study the MSDN page Error Raising and Handling Guidelines. However, disregard the rule that new exception classes should derive from the ApplicationException class, and stick with the code analysis rule CA1058: Types should not extend certain base types.


Share: twitter | facebook   Action: used | fork | flag

.net

Mark '.net' tag as 'like'

Mark '.net' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

exceptions

Mark 'exceptions' tag as 'like'

Mark 'exceptions' tag as 'ignore'


 @mliversage "Code Contributor"
101
July 12, 2010 11:18 PM
edited July 12, 2010 11:21 PM

Fork

 Custom C# exception class -  @mliversage Monday 12, 2010 11:18 PM


0 Feedback


You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010