Exception handling in asp.net

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. 

The different approaches in ASP.Net to handle exceptions
  1. Use exception-handling structures to deal with exceptions within the scope of a procedure.
    try { ---- }
    catch { ---- }
    finally { -----}

  2. Use error events to deal with exceptions within the scope of an object.
    Page_Error - The Page_Error event handler provides a way to trap errors that occur at the page level.
    Application_Error - Application_Error event handler to trap errors that occur at the application level.

  3. Custom error pages to display custom error pages for unhandled exceptions at application level.
<customErrors mode="RemoteOnly" defaultRedirect="~/errors/GeneralError.aspx" redirectMode="ResponseRewrite" /> 

The ResponseRewrite mode allows us to load the Error Page without redirecting the browser, so the URL stays the same, and importantly for me, exception information is not lost.

If a code in a try block causes an exception, control flow passes to immediate catch or finally block. Finally block is mainly used to free resources used within the try block.(Ex: closing db connections, and file connection etc.). A finally block will always be executed irrespective of whether an exception has occurred or not.


Comments

Popular posts from this blog

Data Bound Controls in ASP.Net - Part 4 (FormView and DetailsView controls)

ASP.net: HttpHandlers

The Clickjacking attack and X-Frame-Options