Exceptions in Web Apps?

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Exceptions in Web Apps?

    Hi,

    How does one handle exceptions in a Web application? For
    instance, I have a Page_Load method that throws an
    ArgumentNullException on some ocassions and I have set
    the Web.config file with a section like so:

    <customErrors mode="On" defaultRedirect="ErrorForm.aspx"/>

    From that page, how can I determine what kind of
    exception (and the exception object itself) was thrown?

    Am I approaching the whole issue incorrectly?
    If so, could somebody tell me how are exception handled
    in Web apps?

    Thanks a lot,
    Juan Dent
    Juan Dent Guest

  2. Similar Questions and Discussions

    1. Call windows apps from web apps
      I have 2 apps windows and web apps. I would like to call a windows app (.exe) from web apps. How do I do this?
    2. exceptions
      Hi, I'm puzzled about how to associate custom information with an Exception class I am throwing. In Java, I would do this MyException e =...
    3. Web enabled apps/Thin client apps
      I'm writing an article and I need your informed opinions, so I ask you this: 1. Doesn't the web-enabled app generally run slower for one reason or...
    4. Web Service Exceptions
      Hi Chris, You don't need to serialize the Web service SoapException yourself - it's done for you. All you need to do is to catch the original...
    5. [PHP-DEV] exceptions question
      You should not use exceptions for program logic. Exceptions are use to handle exceptional cases and the overhead of handling an exception may be...
  3. #2

    Default Re: Exceptions in Web Apps?

    "Juan Dent" <juan5@dev.com> wrote in message
    news:036b01c35309$aded3180$a501280a@phx.gbl...
    | Hi,
    |
    | How does one handle exceptions in a Web application? For
    | instance, I have a Page_Load method that throws an
    | ArgumentNullException on some ocassions and I have set
    | the Web.config file with a section like so:
    |
    | <customErrors mode="On" defaultRedirect="ErrorForm.aspx"/>
    |
    | From that page, how can I determine what kind of
    | exception (and the exception object itself) was thrown?

    I can recommend to handle errors in Application_Error event handler and to
    use the error page only to inform user.

    The following is code I use for all my applications. It checks if custom
    errors are enabled (if no, it expects that's a development server and does
    not do anything) and then sends all available details by e-mail to person
    specified by web.config variable "Mail.Webmaster". Written in VB.NET:

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    '-- Ignore HTTP errors (ie. 404) and damaged viewstate
    If Server.GetLastError.GetType() Is GetType(System.Web.HttpException)
    Then Return
    If Server.GetLastError.ToString.IndexOf("View State is invalid") > -1
    Then Return
    If Server.GetLastError.ToString.IndexOf("viewstate is invalid") > -1
    Then Return

    '-- Check if custom errors are enabled
    Dim xDoc As New System.Xml.XmlDocument
    xDoc.Load(Server.MapPath("/web.config"))
    If Not
    xDoc.SelectSingleNode("/configuration/system.web/customErrors[@mode='Off']")
    Is Nothing Then Return

    '-- Generate text of e-mail message
    Dim SB As New System.Text.StringBuilder
    Dim S As String
    SB.Append("Time:\n" & Now.ToString("yyyy-MM-dd HH:mm:ss"))
    SB.Append("\n\nVersion:\n" &
    System.Reflection.Assembly.GetExecutingAssembly.Ge tName.Version.ToString())
    SB.Append("\n\nRequested URL:\n" & Request.Url.ToString)
    SB.Append("\n\nException:\n" & Server.GetLastError.ToString)
    SB.Append("\n\nRemote host:\n" & Request.UserHostAddress)
    SB.Append("\n\nUser agent:\n" & Request.UserAgent)
    SB.Append("\n\nAuthentication:\n" &
    DirectCast(IIf(Request.IsAuthenticated, "yes, as " &
    Context.User.Identity.Name, "no"), String))
    SB.Append("\n\nServer variables:")
    For Each S In Request.ServerVariables.Keys
    SB.Append("\n" & S & " = " & Request.ServerVariables(S))
    Next
    SB.Append("\n\nPOST data available:")
    For Each S In Request.Form.Keys
    SB.Append("\n" & S & " = " & Request.Form(S))
    Next

    '-- Send e-mail message
    Dim MX As New System.Web.Mail.MailMessage
    MX.From = "WWW-Daemon <www-daemon@altaircom.net>"
    MX.To = ConfigurationSettings.AppSettings("Mail.Webmaster" )
    MX.Subject = "Error in " & Request.Url.Host
    MX.Body = SB.ToString.Replace("\n", vbCrLf)
    System.Web.Mail.SmtpMail.Send(MX)
    End Sub

    --
    Michal A. Valasek, Altair Communications, [url]http://www.altaircom.net[/url]
    Please do not reply to this e-mail, for contact see [url]http://www.rider.cz[/url]


    Michal A. Valasek Guest

  4. #3

    Default Re: Exceptions in Web Apps?

    it's not a good idea to let the application object handle page exceptions.
    why have the exception bubble to the top? by the time the exception reaches
    the top, the context of the error is properly lost and the application
    object cannot possibly remedy the situation. Instead, you should attempt to
    always handle the exceptions at the point at which they occur in the code
    with a catch block or next at the page level by chaining to the error event
    like so this.error += new handler(page_error).
    in the page_error handler you put code to handle the exception at the page
    level. all exceptions occuring in the page will then be handled at the page
    level assuming that your try catch block failed to catch the exception at
    the code level. if you determine then that you cannot handle the exception
    at the page level because of context, then you may rethrow the error to the
    next level, possibly the appdomain layer, session layer and then to the
    application layer. this layered approach provides a structured way in
    allowing each layer a chance to handle the exception or pass it on up to the
    caller.
    "Michal A. Valasek" <news@altaircom.net> wrote in message
    news:#MXi5z1UDHA.2252@TK2MSFTNGP10.phx.gbl...
    > "Juan Dent" <juan5@dev.com> wrote in message
    > news:036b01c35309$aded3180$a501280a@phx.gbl...
    > | Hi,
    > |
    > | How does one handle exceptions in a Web application? For
    > | instance, I have a Page_Load method that throws an
    > | ArgumentNullException on some ocassions and I have set
    > | the Web.config file with a section like so:
    > |
    > | <customErrors mode="On" defaultRedirect="ErrorForm.aspx"/>
    > |
    > | From that page, how can I determine what kind of
    > | exception (and the exception object itself) was thrown?
    >
    > I can recommend to handle errors in Application_Error event handler and to
    > use the error page only to inform user.
    >
    > The following is code I use for all my applications. It checks if custom
    > errors are enabled (if no, it expects that's a development server and does
    > not do anything) and then sends all available details by e-mail to person
    > specified by web.config variable "Mail.Webmaster". Written in VB.NET:
    >
    > Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    > '-- Ignore HTTP errors (ie. 404) and damaged viewstate
    > If Server.GetLastError.GetType() Is GetType(System.Web.HttpException)
    > Then Return
    > If Server.GetLastError.ToString.IndexOf("View State is invalid") > -1
    > Then Return
    > If Server.GetLastError.ToString.IndexOf("viewstate is invalid") > -1
    > Then Return
    >
    > '-- Check if custom errors are enabled
    > Dim xDoc As New System.Xml.XmlDocument
    > xDoc.Load(Server.MapPath("/web.config"))
    > If Not
    >
    xDoc.SelectSingleNode("/configuration/system.web/customErrors[@mode='Off']")
    > Is Nothing Then Return
    >
    > '-- Generate text of e-mail message
    > Dim SB As New System.Text.StringBuilder
    > Dim S As String
    > SB.Append("Time:\n" & Now.ToString("yyyy-MM-dd HH:mm:ss"))
    > SB.Append("\n\nVersion:\n" &
    >
    System.Reflection.Assembly.GetExecutingAssembly.Ge tName.Version.ToString())
    > SB.Append("\n\nRequested URL:\n" & Request.Url.ToString)
    > SB.Append("\n\nException:\n" & Server.GetLastError.ToString)
    > SB.Append("\n\nRemote host:\n" & Request.UserHostAddress)
    > SB.Append("\n\nUser agent:\n" & Request.UserAgent)
    > SB.Append("\n\nAuthentication:\n" &
    > DirectCast(IIf(Request.IsAuthenticated, "yes, as " &
    > Context.User.Identity.Name, "no"), String))
    > SB.Append("\n\nServer variables:")
    > For Each S In Request.ServerVariables.Keys
    > SB.Append("\n" & S & " = " & Request.ServerVariables(S))
    > Next
    > SB.Append("\n\nPOST data available:")
    > For Each S In Request.Form.Keys
    > SB.Append("\n" & S & " = " & Request.Form(S))
    > Next
    >
    > '-- Send e-mail message
    > Dim MX As New System.Web.Mail.MailMessage
    > MX.From = "WWW-Daemon <www-daemon@altaircom.net>"
    > MX.To = ConfigurationSettings.AppSettings("Mail.Webmaster" )
    > MX.Subject = "Error in " & Request.Url.Host
    > MX.Body = SB.ToString.Replace("\n", vbCrLf)
    > System.Web.Mail.SmtpMail.Send(MX)
    > End Sub
    >
    > --
    > Michal A. Valasek, Altair Communications, [url]http://www.altaircom.net[/url]
    > Please do not reply to this e-mail, for contact see [url]http://www.rider.cz[/url]
    >
    >

    Alvin Bruney Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139