Saturday, February 10, 2007

magic variables to help in exception debugging

This article applies to C#, J#, and Visual Basic.
I read about these variables a long time ago in some blog, but I could not find anything about those hidden little things, mainly the problem was that they are not called magic or hidden, they are documented and are called Pseudovariables;

Pseudovariables are terms used to display certain information in a variable window or the QuickWatch dialog box. You can enter a pseudovariable the same way you would enter a normal variable. Pseudovariables are not variables, however, and do not correspond to variable names in your program.

Anyway, I don't think many people know about them, here they are:
  • $exception: Displays information on the last exception. If no exception has occurred, evaluating $exception displays an error message.
    In Visual C# only, when the Exception Assistant is disabled, $exception is automatically added to the Locals window when an exception occurs.
  • $user: Displays a structure with account information for the account running the application. For security reasons, the password information is not displayed
when you are debugging an exception you might have some code like this:

try {
//some code
} catch (Exception e) {
//don't really need e here, just to get the exception information on the Watch window
}

you could write this code as:
try {
//some code
catch { } //put the breakpoint on this line, inspect $exception (Debug, Watch, Watch 1, type $exception there)

and get the same information.

The $user pseudovariable is pretty self explanatory.

in native code you have a few more pseudovariables available, check the link if you are interested.

those little things...

No comments: