How can I cast an exception to its own type when I don't know what its type is?

R

Rob Richardson

Greetings!

I am working on an application that targets a Pocket PC running Windows CE
and SQL Server CE. Almost all functions in the application use a Try block
with a Catch block that looks like this:

Try
TryToDoIt()
Catch e as Exception
LogTheError(e)
Throw e
End Try

I would like a little more intelligence in this. I wrote a little routine
that generates a message showing expanded error information if the exception
is an SqlCeException. But I'd rather not add a second catch block to all of
the places where an SqlCeException could be thrown. For one thing, that
would be labor-intensive. For another thing, if this app is ever migrated
to a desktop, I'd have to add another catch block for SqlExceptions, and if
it's ported to Microsoft Access (God forbid), I'd have to add
OleDbExceptions, and so on.

So, I was hoping to do something like this:

Throw CType(e, TypeName(e))

so that when it got up to the next call in the call stack, it would be an
SqlCeException instead of just an Exception. Of course, that doesn't work,
since the type name has to be a type instead of a character string that
happens to hold a name type. Is there a way to do what I want?

Thanks very much!

Rob
 
J

Jay B. Harlow [MVP - Outlook]

Rob,
Try
TryToDoIt()
Catch e as Exception
LogTheError(e)
Throw e
End Try
If you use "Throw e" your stack trace will be lost, if you simple use
"Throw" the stack trace will be preserved.

Using either "Throw e" or "Throw" above will throw the type of exception
that you originally caught. If you caught a SqlCeException, it will throw a
SqlCeException. To throw a different type of exception you would need to use
"Throw New DifferentTypeOfExcpetion(e)". Note I am passing the exception
that I caught as an Inner Exception, so outer routines know what the real
exception was.


Note I normally do my logging in a global exception handler. I use
try/finally more then I use try/catch. I only use try/catch when there is
something specific that I need to do with the exception, otherwise I let my
global exception handlers handle the exception. In other words you don't
need to include Try/Catch in all of your routines, you only need to have
your Global Exception handler (below) to log your exceptions, then include
Try/Finally where you need to close resources & such.


Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all

uncaught exceptions from any form/control event handlers.

Note David has some excellent comments on argument validation to your class
libraries. Especially if those class libraries are going to be used outside
of your current solution.

Hope this helps
Jay
 
G

Guest

Just curious, but I use OnError GoTo's a lot and they seem to catch most all
exceptions (haven't had the joy of finding one that didn't get caught yet.)
What is the downside to using OnError GoTo's?
 
R

rob willaar

Hi Jay,

I use this code in new() of the form:
#If Not Debug Then

AddHandler Application.ThreadException, AddressOf fErr.DisplayError

#End If

Were fErr.DisplayError(ByVal sender As Object, ByVal t As
System.Threading.ThreadExceptionEventArgs) handles the unexpected exception.



The big advantage to me is:

- I don't have a lot of OnError GoTo code

- This code is part of a form i use derive from. Even if a add a new
function within my derived form, unexpected exceptions will be handeled
(logged).



Louis
 
J

Jay B. Harlow [MVP - Outlook]

Dennis,
There have been numerous discussions on using On Error Goto verses Try/Catch
& Try/Finally.

Try the following thread:

http://groups.google.com/groups?hl=...3780&seekm=OlhL4whZBHA.2012@tkmsftngp04#link1

Title "What's with On Error Resume Next???" around 8 Nov 2001.

A handful of articles on the subject:

http://msdn.microsoft.com/vbasic/de...-us/dv_vstechart/html/vbnet_errorhandling.asp


See the "Use Structured Exception Handling Rather Than On Error Statement"
section of:
http://msdn.microsoft.com/vstudio/u...tml/vbtchmicrosoftvisualbasicnetinternals.asp

See the "Exception Handling" section of:
http://msdn.microsoft.com/vstudio/u...rary/en-us/dv_vstechart/html/vbtchperfopt.asp

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top