Home All Groups Group Topic Archive Search About

Recast an exception to its original type

Author
30 Mar 2006 2:12 PM
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

Author
30 Mar 2006 3:34 PM
Larry Lard
Jonas wrote:
> 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).

--
Larry Lard
Replies to group please
Author
30 Mar 2006 4:13 PM
Phill W.
"Jonas" <Jonas@nospam.pl> wrote in message
news:uW$$1QAVGHA.5332@tk2msftngp13.phx.gbl...

> 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.
Author
31 Mar 2006 6:54 AM
Jonas
Show quote Hide quote
"Phill W." <p-.a-.w-a-r-d@o-p-e-n.a-c.u-k> wrote in message
news:e0h00o$i27$1@yarrow.open.ac.uk...
>
> "Jonas" <Jonas@nospam.pl> wrote in message
> news:uW$$1QAVGHA.5332@tk2msftngp13.phx.gbl...
>
>> 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.

The problem is that if you to a .ToString() on a SqlException, you won't get
the Procedure, LineNumber and Server properties.

>
>> 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.

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