1 / 25

EXCEPTION HANDLING IN .NET

EXCEPTION HANDLING IN .NET. The exceptions are anomalies that occur during the execution of a program. They can be because of user, logic or system errors.

xenia
Download Presentation

EXCEPTION HANDLING IN .NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EXCEPTION HANDLING IN .NET

  2. The exceptions are anomalies that occur during the execution of a program. • They can be because of user, logic or system errors. • If a user do no provide a mechanism to handle these anomalies, the .NET runtime environment provide a default mechanism, which terminates the program execution.

  3. Exception Handling is an in build mechanism in .NET framework to detect and handle run time errors.

  4. Unhandled Exceptions- Redirecting the user to an error page. • Page level • Application level

  5. Page Level • <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication.WebForm1“ errorPage=“/pageError/PageError.html” %> • In the web.config file <customErrors mode=“On" />

  6. Application Level • In the web.config page • <customErrors mode=“On|Off|RemoteOnly” defaultRedirect=“url”> <error statusCode=“statuscode” redirect=“url” /> </customErrors > • Example: <customErrors mode=“On” defaultRedirect=“myerror.htm”> <error statusCode=“403” redirect=“authfailed.aspx” /> <error statusCode=“404” redirect=“filenotfound.aspx” /> </customErrors >

  7. Page level would override Application level • Error handlers Application_Errorand Page_Error, will get called before custom Errors is utilized.

  8. Handling Exceptions • HTTP Module level by handling the HttpApplication.Error event. • Application level by handling the Application.Error event. • Page level by handling the Page.Error event. • Locally (method level), where exceptions could be thrown.

  9. HTTP Module Level • Attaching an HTTP Module which would have a handler attached to the Application.Error event.

  10. Application Level • Event : Application.Error • Application_Error Event handler in Global.asax.cs • Inside error handler • Server.GetLastError • Server.ClearError

  11. Page Level • Event : Page.Error • Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Error End Sub • We can do … • Exception ex = Server.GetLastError(); • this.ErrorPage = “/ErrorPages/BaseError.html”;

  12. Local error handling • try-catch-finally

  13. Local Exception Handling in VB.NET • In VB.NET, exceptions are nothing but objects of the type Exception. • The Exception is the ultimate base class for any exceptions in VB.NET. • The VB.NET itself provides couple of standard exceptions. • Or even the user can create their own exception classes.

  14. Object Exception IndexOutOfRangeException NullReferenceException ArgumentException ExternalException SystemException ApplicationException Exception Hierarchy

  15. Exception Class - Properties • StackTrace • InnerException • Message • HelpLink

  16. General form Try ‘ statement which can cause an exception. Catch x As Type When filter-condition ‘ Statements for handling the exception. Finally ‘ Any cleanup code End Try

  17. Uncaught Exceptions

  18. Multiple Catch Blocks Dim x AsInteger = 0 Dim div AsInteger = 0 Try div = 100 / x Catch de As DivideByZeroException ‘ Statements for handling the exception. Catch ee As Exception ‘ Statements for handling the exception. Finally ‘ Any cleanup code End Try

  19. Catching all Exceptions Try ‘Stmts Catch e As Exception ‘Stmts End Try Try ‘Stmts Catch ‘Stmts End Try

  20. User-Filtered Clauses Dim t As Integer Try t = 10 ThrowNew Exception CatchWhen t = 10 t = t + 1 Catch ed As Exception t = t + 1 End Try

  21. Combining Specific Exception & the User-Filtered Clauses Dim t As Integer Try t = 10 Throw New Exception Catch ed As Exception When t = 10 t = t + 1 EndTry

  22. Throwing an Exception • Throw exception_obj • Example Try ThrowNew DivideByZeroException(“Invalid Division”) Catch e As DivideByZeroException ‘Stmts End Try

  23. Re-throwing an Exception Public Class MyTestClass Public Sub Method() Try Dim x As Integer = 0 Dim sum As Integer = 100 / x Catch ex As Exception Throw EndTry EndSub End Class Public Class MyTestClient Public Shared Sub Main() Dim mc As New MyTestClass Try mc.Method() Catch ex As Exception Console.WriteLine("Exception caught here") End Try End Sub End Class

  24. Standard Exceptions • System.OutOfMemoryException • System.NullReferenceException • System.InvalidCastException • System.ArrayTypeMismatchException • System.IndexOutOfRangeException • System.ArithmeticException • System.DevideByZeroException • System.OverFlowException

  25. User-Defined Exception Public Class EmployeeNotFoundException Inherits ApplicationException Public SubNew() End Sub Public Sub New(message As String) MyBase.New(message) End Sub Public Sub New(message As String, inner As Exception) MyBase.New(message,inner) End Sub End Class

More Related