EXCEPTION HANDLING IN .NET

25
EXCEPTION HANDLING IN .NET

description

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. - PowerPoint PPT Presentation

Transcript of EXCEPTION HANDLING IN .NET

Page 1: EXCEPTION HANDLING IN .NET

EXCEPTION HANDLING IN .NET

Page 2: 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.

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.

Page 3: EXCEPTION HANDLING IN .NET

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

Page 4: EXCEPTION HANDLING IN .NET

Unhandled Exceptions- Redirecting the user to an error page.

Page level Application level

Page 5: EXCEPTION HANDLING IN .NET

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" />

Page 6: EXCEPTION HANDLING IN .NET

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 >

Page 7: EXCEPTION HANDLING IN .NET

Page level would override Application level

Error handlers Application_Error and Page_Error, will get called before custom Errors is utilized.

Page 8: EXCEPTION HANDLING IN .NET

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.

Page 9: EXCEPTION HANDLING IN .NET

HTTP Module Level

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

Page 10: EXCEPTION HANDLING IN .NET

Application Level

Event : Application.Error Application_Error Event handler in

Global.asax.cs Inside error handler

Server.GetLastError Server.ClearError

Page 11: EXCEPTION HANDLING IN .NET

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”;

Page 12: EXCEPTION HANDLING IN .NET

Local error handling

try-catch-finally

Page 13: EXCEPTION HANDLING IN .NET

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.

Page 14: EXCEPTION HANDLING IN .NET

Exception Hierarchy

Object

Exception

ApplicationException

SystemException

IndexOutOfRangeException

NullReferenceException

ArgumentException

ExternalException

Page 15: EXCEPTION HANDLING IN .NET

Exception Class - Properties

StackTrace InnerException Message HelpLink

Page 16: EXCEPTION HANDLING IN .NET

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

Page 17: EXCEPTION HANDLING IN .NET

Uncaught Exceptions

Page 18: EXCEPTION HANDLING IN .NET

Multiple Catch Blocks

Dim x As Integer = 0Dim div As Integer = 0Try

div = 100 / xCatch de As DivideByZeroException

‘ Statements for handling the exception.Catch ee As Exception

‘ Statements for handling the exception.Finally

‘ Any cleanup codeEnd Try

Page 19: EXCEPTION HANDLING IN .NET

Catching all Exceptions

Try‘Stmts

Catch‘Stmts

End Try

Try‘Stmts

Catch e As Exception‘Stmts

End Try

Page 20: EXCEPTION HANDLING IN .NET

User-Filtered Clauses

Dim t As Integer Try t = 10 Throw New Exception Catch When t = 10 t = t + 1 Catch ed As Exception t = t + 1 End Try

Page 21: EXCEPTION HANDLING IN .NET

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 End Try

Page 22: EXCEPTION HANDLING IN .NET

Throwing an Exception

Throw exception_obj Example

Try

Throw New DivideByZeroException(“Invalid Division”)Catch e As DivideByZeroException

‘StmtsEnd Try

Page 23: EXCEPTION HANDLING IN .NET

Re-throwing an ExceptionPublic Class MyTestClass Public Sub Method() Try Dim x As Integer = 0 Dim sum As Integer = 100 / x Catch ex As Exception Throw End Try End SubEnd 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 SubEnd Class

Page 24: EXCEPTION HANDLING IN .NET

Standard Exceptions

System.OutOfMemoryException System.NullReferenceException System.InvalidCastException System.ArrayTypeMismatchException System.IndexOutOfRangeException System.ArithmeticException System.DevideByZeroException System.OverFlowException

Page 25: EXCEPTION HANDLING IN .NET

User-Defined ExceptionPublic Class EmployeeNotFoundException

Inherits ApplicationException

Public Sub New()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