CodeCloudy

Azure | .Net | JQuery | Javascript | Umbraco

.Net Error Handling

  • Try/catch – (for page level error)
  • Page_Error: Occurs when an error occurs within the Web page. This event is in the Web form. –  (for page level error)

private void Page_Error(object sender, EventArgs e)
{}

  • Global_Error: Occurs when an error occurs within the application. This event is in the Gloabl.asax file.

private void Page_Error(object sender, EventArgs e)
{}

  • Application_Error: Occurs when an error occurs within the application. This event is in the Gloabl.asax file.

void Application_Error(object sender, EventArgs e)
{}

  • CustomError

<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug=”true” />

<!– Turn on Custom Errors –>
<customErrors mode=”On”
defaultRedirect=”DefaultRedirectErrorPage.aspx”>
<error statusCode=”404″ redirect=”Http404ErrorPage.aspx”/>
</customErrors>

</system.web>
</configuration>

  • HandleErrorAttribute  – Represents an attribute that is used to handle an exception that is thrown by an action method. (MVC specific)  –  (for page level error) http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute.aspx
  • I would use Application_Error for those cases where the page isn’t handling the error for some reason, as a last resort to log the error. If a page can handle its own errors and gracefully deal with them, it should do so in Page_Error.
  • in simple words, if the page can handle it the page will use Page_Error to do it. or else it will be handled by the Application_Error.
  • Methods in the Server object are used to handle the exception in the error events.
    • GetLastError: Gets the last exception that occurred on the server.
    • ClearError: Use this method to handle the exception and stop the error to trigger the subsequent error event or display the error to the user.

		
Advertisements
Leave a comment »