Ask a Question related to ASP.NET General, Design and Development.
-
Juan Dent #1
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
-
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? -
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 =... -
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... -
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... -
[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... -
Michal A. Valasek #2
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
-
Alvin Bruney #3
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...xDoc.SelectSingleNode("/configuration/system.web/customErrors[@mode='Off']")> "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
>System.Reflection.Assembly.GetExecutingAssembly.Ge tName.Version.ToString())> 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" &
>> 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



Reply With Quote

