Exceptions

Have you ever experienced an exception? (See Figure 16-4 for an example.) Of course, I am sure that any exception occurred while you were using an application that someone else wrote. As a kind gesture, you volunteered to help diagnose the problem and correct their unstable application.

A typical dialog box reporting an exception

Figure 16-4. A typical dialog box reporting an exception

Managed exceptions start life as native exceptions. To raise an exception, the CLR calls mscorwks!RaiseTheException. RaiseTheException then calls RaiseException, which is a system-level API. RaiseException assigns all managed exceptions the exception code of E0434F4D. Because all managed exceptions are raised with this same exception code, they are indistinguishable as native exceptions. Fortunately, the first parameter of the RaiseTheException function call is a managed exception object. You can inspect that object for the specifics of the managed exception.

Exception Example

The Store application in the Exceptions folder has been modified to raise an exception when the Add Transactions button is clicked. The objective of this example is to determine the type of exception.

  1. Start the Store application in the Exceptions subfolder. Open WinDbg and attach to the Store program.

  2. Set a breakpoint on the RaiseException API and resume the application:

    0:004> bp Kernel32!RaiseException;g
  3. Click Add Transaction. The exception is raised and the breakpoint is hit in WinDbg. Display the call stack. You should see both RaiseException and RaiseTheExceptionInternalOnly methods on the call stack.

  4. Use the !dumpobj command to dump the first parameter of RaiseTheExceptionInternalOnly, which is the managed exception object. You now have the details of the exception, including the exception type.

The following two commands are helpful in debugging managed exceptions:

  • sxe clr. This command has WinDbg break on all unhandled managed exceptions. You then can display the call stack for the details of the exception. This is an alternative to setting a breakpoint on the RaiseException API.

  • !soe exceptiontype. The Stop On Exception (soe) command is an SOS command. When the specified managed exception occurs, the application is interrupted. Here is an example:

    !soe System.DivideByZeroException
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.186.167