Exception handling in asp.net

NeeleshShukla 8,068 views 17 slides Nov 20, 2012
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

This is a slide for handling all types of exception in .net


Slide Content

Exception Error occurred in execution time Abnormal termination of program Wrong execution result 11/20/2012 1 Presented by: Neelesh Shukla

Is it a best practice to handle every error? No, it is not best practice to handle every error. It degrades the performance. You should use Error Handling in any of following situation otherwise try to avoid it. If you can able to recover error in the catch block To write clean-up code that must execute even if an exception occur To record the exception in event log or sending email. 11/20/2012 2 Presented by: Neelesh Shukla

Exception Handling There are three ways to handle exceptions/errors in ASP.NET 1) Try-Catch block. This is also called Structured Exception Handling (SEH). 2) Error Events. They are page level or application level error events. 3) Custom Error Page. This is used for any unhandled error. 11/20/2012 3 Presented by: Neelesh Shukla

1) What is try Block? Try Block consist of code that might generate error. Try Block must be associated with one or more catch block or by finally block. Try Block need not necessarily have a catch Block associated with it but in that case it must have a finally Block associate with it. 11/20/2012 4 Presented by: Neelesh Shukla

What is catch Block? Catch Block is used to recover from error generated in try Block. In case of multiple catch Block, only the first matching catch Block is executed. When you write multiple catch block you need to arrange them from specific exception type to more generic type. When no matching catch block are able to handle exception, the default behavior of web page is to terminate the processing of the web page. 11/20/2012 5 Presented by: Neelesh Shukla

What is finally Block? Finally Block contains the code that always executes, whether or not any exception occurs. When to use finally Block? - You should use finally block to write cleanup code. i.e. you can write code to close files, database connections, etc. Only One finally block is associated with try block. Finally block must appear after all the catch block. If there is a transfer control statement such as goto , break or continue in either try or catch block the transfer happens only after the code in the finally block is executed. If you use transfer control statement in finally block, you will receive compile time error. 11/20/2012 6 Presented by: Neelesh Shukla

Example of Try-Catch-Finally Try { -------- -------- -------- } Catch(Exception ex) { ------- ------- } Finally { ---- } 11/20/2012 7 Presented by: Neelesh Shukla

System-Defined Exception DivideByZero Exception NullReference Exception IndexOutOfRange Exception ArrayTypeMismatch Exception Arithmetic Exception Exception etc. 11/20/2012 8 Presented by: Neelesh Shukla

Exception class object properties Message: Get the error message. Source: Set or get the name of the application or object that causes the exception. StackTrace : Get a string representation of the frame on call stack at the time of current exception thrown. TargetSite : Get the current method that throws exception. InnerException : Get the system.exception instance that causes the current exception. 11/20/2012 9 Presented by: Neelesh Shukla

In general, you will not be able to plan for, catch and recover from every possible exception that could occur within a page. ASP.NET helps by offering two techniques to handle page-level errors : Error Events Custom Error Page. 11/20/2012 10 Presented by: Neelesh Shukla

2)Error Events They are page level or application level error events:- Page_Error () Application_Error () 11/20/2012 11 Presented by: Neelesh Shukla

Page_Error () private void Page_Error (object sender, EventArgs e) { Exception ex = Server.GetLastError (); Response.Write ("<h1>An error has occurred</h1>"); Response.Write ("<h2>" + ex.Message + "</h2>"); Response.Write ("<pre>" + ex.StackTrace + "</pre>"); Context.ClearError (); } Note : This event fire if the page control is not disposed. 11/20/2012 12 Presented by: Neelesh Shukla

Application_Error () The Error event of the Application class is fired when an exception is left unhandled. The handler is usually found in Global.asax void Application_Error (object sender, EventArgs e) { // Code that runs when an unhandled error occurs System.Exception ex = Context.Server.GetLastError (); ExceptionLog.MessageDetails (ex); // Code for handling error on Application level Response.Write ("Handled error from Application < br >"); Server.ClearError (); } Note : In the code above that ClearError () method should always be called after the exception has been handled. If the ClearError () has not been cleared, the exception will still show up on the client's browser. 11/20/2012 13 Presented by: Neelesh Shukla

3) Custom Error Page First change the Application_Error method to the following: protected void Application_Error (object sender, EventArgs e) {      System.Exception ex = Context.Server.GetLastError (); ExceptionLog.MessageDetails (ex); } Notice that I have removed the Server.ClearError (); Then add a customErrors section to your web.config file. This should be nested within the system.web element Add a new custom error page “Error.aspx” into your project to display a custom Error page, if any unhandled error occurs. 11/20/2012 14 Presented by: Neelesh Shukla

<system.web>       .....     < customErrors mode="On" defaultRedirect ="Error.aspx"> //<error statusCode ="404" redirect="Error404.aspx" /> </ customErrors > </system.web> Note: Details of status code can be found at http://httpstatus.es To customize the default error page, one will have to change the default configuration settings of the application. There are three error modes in which an ASP.Net application can work: Off Mode On Mode RemoteOnly Mode 11/20/2012 15 Presented by: Neelesh Shukla

When the error attribute is set to "Off", ASP.Net uses its default error page for both local and remote users in case of an error. In case of "On" Mode, ASP.Net uses user-defined custom error page instead of its default error page for both local and remote users. If a custom error page is not specified, ASP.Net shows the error page describing how to enable remote viewing of errors. The Error mode attribute determines whether or not an ASP.Net error message is displayed. By default, the mode value is set to " RemoteOnly ". 11/20/2012 16 Presented by: Neelesh Shukla

Thank you 11/20/2012 17 Presented by: Neelesh Shukla