473,396 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2010
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daylor | last post by:
first of all , PLEASE,say somthing..about my post. any reply will be nice to read. i have vb.app , with 2 appdomains. (now, in the next section , ill write "appdomain create" ,means a code in...
3
by: Ole Hanson | last post by:
Hi Trying to cast an exception to the correct type, I encounter some "strange" experiences: The compiler complains that the variable "t" is unknown in line 2 (Exception trueException =...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
10
by: Arjen | last post by:
Hello, Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
15
by: David | last post by:
Hi, I have built a web application that will be a very high profile application. We had tested it, demonstrated it and shown that it all works. On a dress rehearsal run through, it failed...
5
by: UJ | last post by:
I have a try catch where I don't know all of the exceptions that can be thrown (I'm calling a web service in the try) and what I would ideally like to do is a catch all but then look at the type of...
14
by: budy_ludy | last post by:
Hi All, I am new to vb .net, I have an ArrayList and i store class objects in it, and later i want to retrieve each ArrayList items and type cast to the class, How can it be done ? I used...
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.