Monday, January 14, 2013

The Exception Class in C#.NET


Every exception class derives from the base class System.Exception. The .NET Framework is full of predefined exception classes, such as NullReferenceException, IOException, SqlException, and so on. The Exception class includes the essential functionality for identifying any type of error. Below is a list of its most important members.

HelpLink 
A link to a help document, which can be a relative or fully qualified URL (uniform resource locator) or URN (uniform resource name), such as file:///C:/ACME/MyApp/help.html#Err42. The .NET Framework doesn’t use this property, but you can set it in your custom exceptions if you want to use it in your web page code.

InnerException 
A nested exception. For example, a method might catch a simple file IO (input/output) error and create a higher-level “operation failed” error. The details about the original error could be retained in the InnerException property of the higher-level error.

Message 
A text description with a significant amount of information describing the problem.

Source 
The name of the application or object where the exception was raised.

StackTrace 
A string that contains a list of all the current method calls on the stack, in order of most to least recent. This is useful for determining where the problem occurred.

TargetSite 
A reflection object (an instance of the System.Reflection.MethodBase class) that provides some information about the method where the error occurred. This information includes generic method details such as the method name and the data types for its parameter and return values. It doesn’t contain any information about the actual parameter values that were used when the problem occurred.

GetBaseException() 
A method useful for nested exceptions that may have more than one layer. It retrieves the original (deepest nested) exception by moving to the base

When you catch an exception in an ASP.NET page, it won’t be an instance of the generic System.Exception class. Instead, it will be an object that represents a specific type of error. This object will be based on one of the many classes that inherit from System.Exception. These include diverse classes such as DivideByZeroException, ArithmeticException, IOException, SecurityException, and many more. Some of these classes provide additional details about the error in additional properties.

Visual Studio provides a useful tool to browse through the exceptions in the .NET class library. Simply select Debug -> Exceptions from the menu (you’ll need to have a project open in order for this to work). The Exceptions dialog box will appear. Expand the Common Language Runtime Exceptions group, which shows a hierarchical tree of .NET exceptions arranged by namespace (see Figure).


The Exceptions dialog box allows you to specify what exceptions should be handled by your code when debugging and what exceptions will cause Visual Studio to enter break mode immediately. That means you don’t need to disable your error-handling code to troubleshoot a problem. For example, you could choose to allow your program to handle a common FileNotFoundException (which could be caused by an invalid user selection) but instruct Visual Studio to pause execution if an unexpected DivideByZero exception occurs.

To set this up, add a check mark in the Thrown column next to the entry for the System.DivideByZero exception. This way, you’ll be alerted as soon as the problem occurs. If you don’t add a check mark to the Thrown column, your code will continue, run any exception handlers it has defined, and try to deal with the problem. You’ll be notified only if an error occurs and no suitable exception handler is available.

No comments:
Write comments
Recommended Posts × +