Error Handling - Throw New Exception - what next?

elmowatson

Member
Joined
Jun 5, 2007
Messages
17
Programming Experience
1-3
VB.Net 2010
OK - I have an EXE that has several separate classes
I was always told that, in the .vb files, if you wanted Try/Catch error handling that, in the Catch ex as Exception area to 'Throw' a new Exception.

First question - then what? Where do I pop up a message box for the client (or do something) and not crash the app?

Second Question - I put in the following statement:
Throw New ApplicationException("Failed to find record.", ex)

The program seemed to be crashed at the End Try statement (when I debugged it)
 
Exceptions is a two-headed beast, there are lots of do and don't, mostly the latter. Generally you can always Try, but if you don't think you can handle it you should not Catch, only skip remaining code block and possibly clean up in Finally.

Handling it means fixing the problem and continue, or that avoiding it (Catch and swallow) has no consequences for program flow - no Throw in either case, but it may imply a friendly message to user about what is going on if the requested action could not complete for anticipated reasons (specific exceptions).

When caugth but not handled you should re-Throw, either an empty Throw that passes on the current Exception or a new Exception with additional info and the former Exception as inner (*) as in your example.

When it comes to client application level, I would not throw an exception to the user, but instead show a user friendly message(box) that explains the problem in simple terms, minimally a messagebox with the Exception.Message only. Dependingly application should continue or shut down, for example when application state is unrecoverable or unknown and may be exploited. This can be done where it occurs, usually in UI handlers, or let it bubble to the UnhandledException handler My.Application.UnhandledException Event, this is last instance before the Exception is actually thrown to user and application crashes.

These two help pages are main entries to lots of information about exceptions, and there is also a lot out on the web about this topic.
Design Guidelines for Exceptions
Handling and Throwing Exceptions

* A note about InnerException, you don't see much of this in the .Net Framework itself, for the most part exceptions are single instances with no inner. Looking at a stack trace you see the exceptions usually bubbles through several methods, so these either have no Try, no Catch, or an empty Throw.
 
Back
Top