Recast an exception to its original type

J

Jonas

Hi!

I have a general error logging routine that takes a parameter of type
Exception. But if I send in a SqlException, I loose all the Sql-specific
information as the variable is casted to an ordinary Exception. How do I
recast it to its original type without having to hard-code all different
types of exceptions with "typeOf exception Is
System.Data.SqlClient.SqlException" ?

CType(myExp, System.Type(myExp.GetType.FullName))

Something like this above, but it doesn't work because the second argument
returns Nothing.

Any tips?

Brgds

Jonas
 
L

Larry Lard

Jonas said:
Hi!

I have a general error logging routine that takes a parameter of type
Exception. But if I send in a SqlException, I loose all the Sql-specific
information as the variable is casted to an ordinary Exception.

You lose *access* to that information, yes.
How do I
recast it to its original type without having to hard-code all different
types of exceptions with "typeOf exception Is
System.Data.SqlClient.SqlException" ?

CType(myExp, System.Type(myExp.GetType.FullName))

Something like this above, but it doesn't work because the second argument
returns Nothing.

The second argument to CType is a type, yes, but easily specified:

If TypeOf ex Is SqlException Then
Dim sqlex As SqlException = DirectCast(ex, SqlException)
'...

In this case I suggest you use DirectCast rather than CType (once you
have established that ex *is* a SqlException, of course) since this is
a cast rather than a convert (It doesn't make much difference, just
helps the syntax reflect the semantics).
 
P

Phill W.

I have a general error logging routine that takes a parameter of type
Exception. But if I send in a SqlException, I loose all the Sql-specific
information as the variable is casted to an ordinary Exception.

How are you using the Exception passed to this routine?

Personally, I always use ...

ex.ToString()

.... which pulls out the Message and Stack Trace and anything else that
whoever /wrote/ the Exception class chose to put into their ToString
method.
How do I recast it to its original type without having to hard-code all
different types of exceptions with "typeOf exception Is
System.Data.SqlClient.SqlException" ?

AFAIK, you can't.
You can only cast to a Type known at compile-time, which means you
have to code for each and every one.

This is one of the drawbacks with "generic" error handling routines.

HTH,
Phill W.
 
J

Jonas

Phill W. said:
How are you using the Exception passed to this routine?

Personally, I always use ...

ex.ToString()

... which pulls out the Message and Stack Trace and anything else that
whoever /wrote/ the Exception class chose to put into their ToString
method.

The problem is that if you to a .ToString() on a SqlException, you won't get
the Procedure, LineNumber and Server properties.
AFAIK, you can't.
You can only cast to a Type known at compile-time, which means you
have to code for each and every one.

This is one of the drawbacks with "generic" error handling routines.

I guess I just have to do some special coding to get the Sql-specific
properties, there's no other way around it.

Thanks

Jonas
 

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