By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,343 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,343 IT Pros & Developers. It's quick & easy.

Recast an exception to its original type

P: n/a
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
Mar 30 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

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

Mar 30 '06 #2

P: n/a

"Jonas" <Jo***@nospam.pl> wrote in message
news:uW**************@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.
Mar 30 '06 #3

P: n/a

"Phill W." <p-***********@o-p-e-n.a-c.u-k> wrote in message
news:e0**********@yarrow.open.ac.uk...

"Jonas" <Jo***@nospam.pl> wrote in message
news:uW**************@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
Mar 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.