|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Recast an exception to its original typeI 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 Jonas wrote:
> Hi! You lose *access* to that information, yes.> > 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 The second argument to CType is a type, yes, but easily specified:> 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. 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 "Jonas" <Jonas@nospam.pl> wrote in message How are you using the Exception passed to this routine?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. 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 AFAIK, you can't.> different types of exceptions with "typeOf exception Is > System.Data.SqlClient.SqlException" ? 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.
Show quote
Hide quote
"Phill W." <p-.a-.w-a-r-d@o-p-e-n.a-c.u-k> wrote in message The problem is that if you to a .ToString() on a SqlException, you won't get 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 Procedure, LineNumber and Server properties. > I guess I just have to do some special coding to get the Sql-specific >> 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. properties, there's no other way around it. Thanks Jonas
select text in textbox
call method on passed form VB .Net with ADSI problem Capitalize Variable Names Convert IsMissing, IsNull, VBempty to vb.net Easily upgrade B to VB.NET Basic Question - Working with forms Operators do not work in VB.NET with a C# referenced assembly 3rd party grid control in Visual Basic.net 2005 Showing a web page in VB.Net form? |
|||||||||||||||||||||||