Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Exception Class

Represents errors that occur during application execution.

For a list of all members of this type, see Exception Members.

System.Object
   System.Exception
      Derived classes

[Visual Basic]
<Serializable>
<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class Exception
   Implements ISerializable
[C#]
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Exception : ISerializable
[C++]
[Serializable]
[ClassInterface(ClassInterfaceType::AutoDual)]
public __gc class Exception : public ISerializable
[JScript]
public
   Serializable
 ClassInterface(ClassInterfaceType.AutoDual)
class Exception implements ISerializable

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

This class is the base class for all exceptions. When an error occurs, either the system or the currently executing application reports it by throwing an exception containing information about the error. Once thrown, an exception is handled by the application or by the default exception handler.

The common language runtime provides an exception handling model that is based on the representation of exceptions as objects, and the separation of program code and exception handling code into try blocks and catch blocks, respectively. There can be one or more catch blocks, each designed to handle a particular type of exception, or one block designed to catch a more specific exception than another block.

If an application handles exceptions that occur during the execution of a block of application code, the code must be placed within a try statement. Application code within a try statement is a try block. Application code that handles exceptions thrown by a try block is placed within a catch statement, and is called a catch block. Zero or more catch blocks are associated with a try block, and each catch block includes a type filter that determines the types of exceptions it handles.

When an exception occurs in a try block, the system searches the associated catch blocks in the order they appear in application code, until it locates a catch block that handles the exception. A catch block handles an exception of type T if the type filter of the catch block specifies T or any type that T derives from. The system stops searching after it finds the first catch block that handles the exception. For this reason, in application code, a catch block that handles a type must be specified before a catch block that handles its base types, as demonstrated in the example that follows this section. A catch block that handles System.Exception is specified last.

If none of the catch blocks associated with the current try block handle the exception, and the current try block is nested within other try blocks in the current call, the catch blocks associated with the next enclosing try block are searched. If no catch block for the exception is found, the system searches previous nesting levels in the current call. If no catch block for the exception is found in the current call, the exception is passed up the call stack, and the previous stack frame is searched for a catch block that handles the exception. The search of the call stack continues until the exception is handled or until no more frames exist on the call stack. If the top of the call stack is reached without finding a catch block that handles the exception, the default exception handler handles it and the application terminates.

Exception types support the following features:

  • Human-readable text that describes the error. When an exception occurs, the runtime makes available a text message to inform the user of the nature of the error and to suggest action to resolve the problem. This text message is held in the Message property of the exception object. During the creation of the exception object, you can pass a text string to the constructor to describe the details of that particular exception. If no error message argument is supplied to the constructor, the default error message is used.
  • The state of the call stack when the exception was thrown. The StackTrace property carries a stack trace that can be used to determine where in the code the error occurs. The stack trace lists all the called methods, and the line numbers in the source file where the calls are made.

Two categories of exceptions exist under the base class Exception:

Exception includes a number of properties that help identify the code location, the type, the help file, and the reason for the exception: StackTrace, InnerException, Message, HelpLink, HResult, Source, and TargetSite.

When a causal relationship exists between two or more exceptions, the InnerException property maintains this information. The outer exception is thrown in response to this inner exception. The code that handles the outer exception can use the information from the earlier inner exception to handle the error more appropriately.

The error message string passed to the constructor during the creation of the exception object should be localized, and can be supplied from a resource file using the ResourceManager. For more information on localized resources, see the System.Resources namespace overview and Packaging and Deploying .NET Framework Application.

To provide the user with extensive information concerning why the exception occurred, the HelpLink property can hold a URL (or URN) to a help file.

Exception uses the HRESULT COR_E_EXCEPTION, which has the value 0x80131500.

For a list of initial property values for an instance of Exception, see the Exception constructors.

Example

[Visual Basic, C#, C++] The following code example demonstrates a catch block that is defined to handle ArithmeticException errors. This catch block also catches DivideByZeroException errors because DivideByZeroException derives from ArithmeticException, and there is no catch block explicitly defined for DivideByZeroException errors.

[Visual Basic] 
Imports System

Class ExceptionTestClass
   
   Public Shared Sub Main()
      Dim x As Integer = 0
      Try
         Dim y As Integer = 100 / x
      Catch e As ArithmeticException
         Console.WriteLine("ArithmeticException Handler: {0}", e.ToString())
      Catch e As Exception
         Console.WriteLine("Generic Exception Handler: {0}", e.ToString())
      End Try
   End Sub 'Main
End Class 'ExceptionTestClass

[C#] 
using System;

class ExceptionTestClass 
{
public static void Main() 
{
   int x = 0;
      try 
      {
         int y = 100/x;
      }
         catch (ArithmeticException e) 
         {
         Console.WriteLine("ArithmeticException Handler: {0}", e.ToString());
         }
            catch (Exception e) 
            {
            Console.WriteLine("Generic Exception Handler: {0}", e.ToString());
            }
      }    
}

[C++] 
#using <mscorlib.dll>

using namespace System;

int main() {
   int x = 0;
   try {
      int y = 100/x;
   } catch (ArithmeticException* e) {
      Console::WriteLine(S"ArithmeticException Handler: {0}", e);
   } catch (Exception* e) {
      Console::WriteLine(S"Generic Exception Handler: {0}", e);
   }
}

[Visual Basic, C#, C++] The C# code has the following output:

[Visual Basic, C#, C++] ArithmeticException Handler:

System.DivideByZeroException: Attempted to divide by zero. at

ExceptionTestClass.Main()

[Visual Basic, C#, C++] The Visual Basic code has the following output:

[Visual Basic, C#, C++] ArithmeticException Handler:

System.OverflowException: Exception of type System.OverflowException was thrown.

at ExceptionTestClass.Main()

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

Exception Members | System Namespace | Handling and Throwing Exceptions | Packaging and Deploying Resources | SystemException | ApplicationException

Show:
© 2014 Microsoft