Adding Custom Info to an Exception

R

Raterus

Hi,

I've got a quick question for anyone that knows something about exception handling. I'm handling most of my errors in Application_Error in global.asax, and I'd like to pass some state information about the application along with the entire error: Say I have the following try/catch block

try
conn.open
cmd.ExecuteNonQuery
Catch (ex as Exception)
throw
Finally
conn.close
End Try

Say I have some information I know about the query that executed at runtime, like the sql query used, values of parameters added, current state of the application, etc, and I wanted to tack these on to the error message, so my application_error Sub can pick up on them. I haven't figured out a good way to do this? I tried adding to the "HelpLink" property, but when I call Server.GetLastError in Application_Error, it isn't there. I also don't want to throw away the Stack Information, so I don't want to use throw(ex). Does anyone know if this is possible? It would look something like this I think.

try
conn.open
cmd.ExecuteNonQuery
Catch (ex as Exception)
ex.HelpLink = "Sql Statement Used: " & sql & "Parameter @a: " & cmd.parameters("@a").value
throw
Finally
conn.close
End Try

I have figured out I can at least add some information to the Context (through Context.Items), but there is no guarantee the object I'm currently executing in has access to this object. And I don't think it would be a good practice to start making calls to HttpContext.Current from within my objects that really don't know anything about asp.net.

Thanks for any help,
--Michael
 
G

Girish Bharadwaj

You can create another Exception derived from base Exception and add the
kind of data you you would like to add. That derived Exception can provide
more information.

--
Girish Bharadwaj
http://msmvps.com/gbvb
Hi,

I've got a quick question for anyone that knows something about exception
handling. I'm handling most of my errors in Application_Error in
global.asax, and I'd like to pass some state information about the
application along with the entire error: Say I have the following try/catch
block

try
conn.open
cmd.ExecuteNonQuery
Catch (ex as Exception)
throw
Finally
conn.close
End Try

Say I have some information I know about the query that executed at runtime,
like the sql query used, values of parameters added, current state of the
application, etc, and I wanted to tack these on to the error message, so my
application_error Sub can pick up on them. I haven't figured out a good way
to do this? I tried adding to the "HelpLink" property, but when I call
Server.GetLastError in Application_Error, it isn't there. I also don't want
to throw away the Stack Information, so I don't want to use throw(ex). Does
anyone know if this is possible? It would look something like this I think.

try
conn.open
cmd.ExecuteNonQuery
Catch (ex as Exception)
ex.HelpLink = "Sql Statement Used: " & sql & "Parameter @a: " &
cmd.parameters("@a").value
throw
Finally
conn.close
End Try

I have figured out I can at least add some information to the Context
(through Context.Items), but there is no guarantee the object I'm currently
executing in has access to this object. And I don't think it would be a
good practice to start making calls to HttpContext.Current from within my
objects that really don't know anything about asp.net.

Thanks for any help,
--Michael
 
I

i. Wiin

I'm not sure of the VB syntax, but you can do something like:

try
{
// some code here
}
catch (Exception ex)
{
// create a new expection and re-throw it up
throw new Exception("My Special Text" + ex.Message);
}

There's a couple of ways to instantiate a new Exception, this is just one
that pre-pends text to the original exception's Message property.

Hi,

I've got a quick question for anyone that knows something about exception
handling. I'm handling most of my errors in Application_Error in
global.asax, and I'd like to pass some state information about the
application along with the entire error: Say I have the following try/catch
block

try
conn.open
cmd.ExecuteNonQuery
Catch (ex as Exception)
throw
Finally
conn.close
End Try

Say I have some information I know about the query that executed at runtime,
like the sql query used, values of parameters added, current state of the
application, etc, and I wanted to tack these on to the error message, so my
application_error Sub can pick up on them. I haven't figured out a good way
to do this? I tried adding to the "HelpLink" property, but when I call
Server.GetLastError in Application_Error, it isn't there. I also don't want
to throw away the Stack Information, so I don't want to use throw(ex). Does
anyone know if this is possible? It would look something like this I think.

try
conn.open
cmd.ExecuteNonQuery
Catch (ex as Exception)
ex.HelpLink = "Sql Statement Used: " & sql & "Parameter @a: " &
cmd.parameters("@a").value
throw
Finally
conn.close
End Try

I have figured out I can at least add some information to the Context
(through Context.Items), but there is no guarantee the object I'm currently
executing in has access to this object. And I don't think it would be a
good practice to start making calls to HttpContext.Current from within my
objects that really don't know anything about asp.net.

Thanks for any help,
--Michael
 
F

Frank Mamone

You can create your own custom exception quite easily and populate it with
whatever you need.

Also, look into the InnerException property.

-Frank

Hi,

I've got a quick question for anyone that knows something about exception
handling. I'm handling most of my errors in Application_Error in
global.asax, and I'd like to pass some state information about the
application along with the entire error: Say I have the following try/catch
block

try
conn.open
cmd.ExecuteNonQuery
Catch (ex as Exception)
throw
Finally
conn.close
End Try

Say I have some information I know about the query that executed at runtime,
like the sql query used, values of parameters added, current state of the
application, etc, and I wanted to tack these on to the error message, so my
application_error Sub can pick up on them. I haven't figured out a good way
to do this? I tried adding to the "HelpLink" property, but when I call
Server.GetLastError in Application_Error, it isn't there. I also don't want
to throw away the Stack Information, so I don't want to use throw(ex). Does
anyone know if this is possible? It would look something like this I think.

try
conn.open
cmd.ExecuteNonQuery
Catch (ex as Exception)
ex.HelpLink = "Sql Statement Used: " & sql & "Parameter @a: " &
cmd.parameters("@a").value
throw
Finally
conn.close
End Try

I have figured out I can at least add some information to the Context
(through Context.Items), but there is no guarantee the object I'm currently
executing in has access to this object. And I don't think it would be a
good practice to start making calls to HttpContext.Current from within my
objects that really don't know anything about asp.net.

Thanks for any help,
--Michael
 
P

Phill. W

Raterus said:
Say I have some information I know about the query that executed
at runtime, like the sql query used, values of parameters added,
current state of the application, etc, and I wanted to tack these on to
the error message, so my application_error Sub can pick up on them.

Derive from System.ApplicationException and add whatever new
information you want as properties, as in (/very/ simply)

Public Class Boom
Inherits System.ApplicationException

Public Sub New( _
ByVal oaInner as Exception _
, ByVal saHelpLink as String
)
Mybase.New( oaInner )
Me.HelpLink = saHelpLink
End Sub

Public HelpLink as String = ""

End Class

Now, in order to make use of this, you can either Throw it yourself,
or Catch another Exception and "wrap" it in your class, as in

Try
conn.Open()
conn.ExecuteNonQuery

Catch ex as Exception
Throw New Boom( ex, "helpstring")

Finally
conn.Close()

End Try

And, to catch it in your global error handler

Try
' Do something that throws your Exception

Catch ex as Boom
' ... Something like ...
Console.Writeline( ex.HelpLink )
Console.Writeline( ex.InnerException.ToString() )
End Try

HTH,
Phill W.
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top