Visual Studio does a great job of catching exceptions, but it's not always as helpful at resolving them. The new Exception Assistant that's hardwired into Visual Studio 2005 gives you a head start.
Note
Stumbled into a head-scratching exception? Visual Studio 2005 gives you a head start for resolving common issues with its Exception Assistant.
You don't need to take any steps to activate the Exception Assistant. Instead, it springs into action as soon as your program encounters an unhandled exception.
To see it in action, you need to create some faulty code. A good test is to add the following event handler to any form, which tries to open a non-existent file:
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim XMLText As String = My.Computer.FileSystem.ReadAllText( _ "c:\FileDoesNotExist") End Sub
Now run the application. When the error occurs, Visual Studio switches into break mode and highlights the offending statement. The Exception Assistant then appears, with a list of possible causes for the problem. Each suggestion appears as a separate link in the pop-up window. If you click one of these links, the full MSDN help topic will appear. Figure 1-7 shows the result with the faulty file-reading code; the Exception Assistant correctly identifies the reason that the attempt to open the file failed.
Note
This example uses a new VB language featureâthe My object. You'll learn much more about My objects in the next chapter.
If you want to see the low-level exception information, click
the View Detail link at the bottom of the window. This pops up a
dialog box with a PropertyGrid
showing all the information of the associated exception object. This
change alone is a great step forward from Visual Studio .NET 2003,
where you needed to write a Catch
exception handler and set a breakpoint to take a look at the
underlying exception object.
...solving complex problems? The Exception Assistant isn't
designed to help you sort through issues of any complexity. Instead,
it works best at identifying the all-too-common "gotchas," such as
trying to use a null reference (usually a result of forgetting to use
the New
keyword) and failing to
convert a data type (often a result of an inadvertent type
cast).
For help in the real world, consult a colleague or one of the many .NET discussion groups. Some good choices include http://lab.msdn.microsoft.com/vs2005/community (for the latest on Visual Basic 2005) andâonce Visual Basic 2005 enters its release phaseâhttp://www.windowsforms.net/Forums (for Windows Forms questions), http://www.asp.net/Forums (for ASP.NET issues), and http://discuss.develop.com/advanced-dotnet.html (for more advanced .NET queries).
Get Visual Basic 2005: A Developer's Notebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.