Adding Custom Info to an Exception

Discussion in 'ASP .Net' started by Raterus, Sep 21, 2004.

  1. Raterus

    Raterus Guest

    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
    Raterus, Sep 21, 2004
    #1
    1. Advertising

  2. 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
    "Raterus" <> wrote in message
    news:OpBdvX%...
    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
    Girish Bharadwaj, Sep 21, 2004
    #2
    1. Advertising

  3. Raterus

    i. Wiin Guest

    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.

    "Raterus" <> wrote in message
    news:OpBdvX%...
    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. Wiin, Sep 21, 2004
    #3
  4. Raterus

    Frank Mamone Guest

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

    Also, look into the InnerException property.

    -Frank

    "Raterus" <> wrote in message
    news:OpBdvX%...
    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
    Frank Mamone, Sep 21, 2004
    #4
  5. Raterus

    Phill. W Guest

    "Raterus" <> wrote in message
    news:OpBdvX%...
    > 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.
    Phill. W, Sep 22, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vasanth
    Replies:
    0
    Views:
    519
    Vasanth
    Jun 28, 2004
  2. Andrea Williams

    Clear the Render info & change to different info

    Andrea Williams, Oct 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,394
    Ken Dopierala Jr.
    Oct 27, 2004
  3. Replies:
    2
    Views:
    558
    sloan
    Aug 18, 2006
  4. Kiran
    Replies:
    0
    Views:
    1,200
    Kiran
    Feb 6, 2005
  5. ilya

    get CPU info, RAM info

    ilya, Apr 8, 2010, in forum: Java
    Replies:
    43
    Views:
    5,913
    Mike Schilling
    Apr 19, 2010
Loading...

Share This Page