Application Exceptions in C# .NET
Application exceptions are custom exceptions and are thrown by the application, not by the CLR. Application exceptions are derived from System.ApplicationException or System.Exception. System.ApplicationException adds nothing to System.Exception. While System.SystemException is a marker for system exceptions, System.ApplicationException brands application exceptions. A custom exception derived from System.Exception accomplishes the same feat. When several custom exceptions are planned, create a custom base exception class to categorize the exceptions. For convenience and maintainability, deploy application exceptions together in a separate assembly.
Do not create an application exception for an existing exception. Research the available system exceptions to avoid replicating an existing exception.
These are the steps for creating an application exception:
-
Name the application exception. As a best practice, the class name should have the Exception suffix, as in ApplicationException.
-
Derive the application exception from System.Exception.
-
Define constructors that initialize the state of the application exception. This includes initializing members inherited from the base class.
-
Within the application exception, refine System.Exception as desired, such as by adding attributes that further delineate this specific exception.
To raise an application exception, use the throw statement. You can also throw system exceptions. Thrown exceptions are considered software exceptions. The CLR treats software exceptions as standard exceptions.
throw syntax:
throw exceptioninstance1;
throw2;
The second syntax is specialized: It is available in the catch block, but nowhere else. This version of the throw statement rethrows as an exception caught in the catch block. However, the best policy is to add additional context to an exception before propagating the exception object. Propagating exceptions is reviewed later in this chapter.
Application exceptions are typically prompted by an exceptional event. What is an exceptional event? A strict definition does not exist. You define the basis of the event using whatever criteria are appropriate. Remember, raising an exception simply for transfer of control or a nonexceptional event is bad policy. In an application, the following could be considered exceptional events where throwing an application exception is warranted:
-
Constructor fails to initialize the state of an object.
-
A property does not pass validation.
-
Null parameters.
-
An exceptional value is returned from a function.
ConstructorException is an application exception. Throw this exception when a constructor fails. It refines the System.Exception base class with name of the type and time of exception. In addition, the Message property is assigned a congruous message. This is the code for the ConstructorException class:
using System;
namespace Examples.Exceptions
{
public class ConstructorException : Exception
{
public ConstructorException(object origin) : this(origin, null)
{
}
public ConstructorException(object origin, Exception innerException) : base("Exception in constructor", innerException)
{
prop_Typename = origin.GetType().Name;
prop_Time = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
}
protected string prop_Typename = null;
public string Typename
{
get
{
return prop_Typename;
}
}
protected string prop_Time = null;
public string Time
{
get
{
return prop_Time;
}
}
}
}
This code uses the ConstructorException class:
using System;
namespace Examples.Exceptions
{
public class Program
{
public static void Main()
{
try
{
ZClass obj = new ZClass();
}
catch (ConstructorException except)
{
Console.WriteLine(except.Message);
Console.WriteLine("Typename: " + except.Typename);
Console.WriteLine("Occured: " + except.Time);
}
Console.ReadKey();
}
}
class ZClass
{
public ZClass()
{
// initialization fails
throw new ConstructorException(this);
}
}
}
-
Archives
- February 2009 (1)
- November 2008 (6)
- October 2008 (4)
- September 2008 (13)
- August 2008 (11)
- July 2008 (29)
- June 2008 (19)
- May 2008 (8)
-
Categories
-
RSS
Entries RSS
Comments RSS

