469,073 Members | 1,808 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,073 developers. It's quick & easy.

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

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
Nov 21 '05 #1
4 1803
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/is...T/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

"Rob Richardson" <no*******@n2net.net> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl... 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

Nov 21 '05 #2
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?

"Jay B. Harlow [MVP - Outlook]" wrote:
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/is...T/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

"Rob Richardson" <no*******@n2net.net> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl...
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


Nov 21 '05 #3
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



"Dennis" <De****@discussions.microsoft.com> schreef in bericht
news:F0**********************************@microsof t.com...
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?

"Jay B. Harlow [MVP - Outlook]" wrote:
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/is...T/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

"Rob Richardson" <no*******@n2net.net> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl...
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


Nov 21 '05 #4
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=e...sftngp04#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/def...orhandling.asp
See the "Use Structured Exception Handling Rather Than On Error Statement"
section of:
http://msdn.microsoft.com/vstudio/us...tinternals.asp

See the "Exception Handling" section of:
http://msdn.microsoft.com/vstudio/us...tchperfopt.asp

Hope this helps
Jay

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
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?

"Jay B. Harlow [MVP - Outlook]" wrote:
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/is...T/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

"Rob Richardson" <no*******@n2net.net> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl...
> 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
>
>


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ole Hanson | last post: by
10 posts views Thread by Arjen | last post: by
5 posts views Thread by Nick Flandry | last post: by
15 posts views Thread by David | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.