Exception handling in ASP .NET

21

description

Exception handling in ASP .NET

Transcript of Exception handling in ASP .NET

Page 1: Exception handling in ASP .NET
Page 2: Exception handling in ASP .NET

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 3: Exception handling in ASP .NET

[email protected]/ nithil padinare

peediyekaltwitter.com/nithilppin.linkedin.com/in/nithil.pp9995223505

Exception Handling in .NET

Page 4: Exception handling in ASP .NET

What is Exception?

• An exception is an event, which occurs during the execution of a program.

• It disrupts the normal flow of the program's instructions

• Exception handling enables programmers to remove error-handling code from the “main line” of the program’s execution.

Page 5: Exception handling in ASP .NET

What is Exception? (cont.)

• Simplify code construction and maintenance

• Allow the problematic situations to be processed at multiple levels

Page 6: Exception handling in ASP .NET

Handling Exceptions

• Catching and Processing Errors

• In C# the exceptions can be handled by the try-catch-finally construction

• Provided by System.Exception

• Enable clear, robust and more fault-tolerant programs

• catch blocks can be used multiple times to process different exception types

Page 7: Exception handling in ASP .NET

Handling Exceptions• Keywords

try {

// includes code in which exception might occur

}

catch (InvalidOperationException) {

//Code to handle the exception

//Must be of class Exception or one that extends it directly or indirectly

}

catch (SomeOtherException) {

// code that recovers from an SomeOtherException

// (or any exception type derived from it)

}

catch {

// code that recovers from any kind of exception

// when you catch any exception, you usually re-throw

throw;

}

finally {

// code that cleans up any operations started

// within the try block.

// (Optional) code present here will always be executed

}

Page 8: Exception handling in ASP .NET

Types of Exceptions

• .NET exceptions inherit from System.Exception

• The system exceptions inherit from System.SystemException, e.g.

– System.ArgumentException

– System.NullReferenceException

– System.OutOfMemoryException

– System.StackOverflowException

• User-defined exceptions should inherit from System.ApplicationException

Page 9: Exception handling in ASP .NET

Common .NET ExceptionsThe CLR generates SystemExceptions, derived from class

Exception, which can occur at any point during program execution.

If a program attempts to access an out-of-range array index, the CLR throws an exception of type IndexOutOfRangeException.

Attempting to use a null reference causes a NullReferenceException.

Page 10: Exception handling in ASP .NET

try block

• A try block contains code that requires common cleanup or exception-recovery operations.

• The cleanup code should be put in a single finally block.

• The exception recovery code should be put in one or more catch blocks.– Create one catch block for each kind of type you want to handle.

• A try block must have at least one catch or finally block.

Page 11: Exception handling in ASP .NET

catch block

• A catch block contains code to execute in response to an exception.

• If the code in a try block doesn’t cause an exception to be thrown, the CLR will never execute the code in any of its catch blocks.

• The catch type must be of type System.Exception or a type that derived from System.Exception

• You can also specify a variable name like catch(Exception e) to access information specific to the exception.

Page 12: Exception handling in ASP .NET

finally blockC# provides the finally block, which is guaranteed to

execute regardless of whether an exception occurs.

If the try block executes without throwing, the finally block executes.

If the try block throws an exception, the finally block still executes regardless of whether the exception is caught.

This makes the finally block ideal to release resources from the corresponding try block.

Page 13: Exception handling in ASP .NET

finally block (conti.)

• Local variables in a try block cannot be accessed in the corresponding finally block, so variables that must be accessed in both should be declared before the try block.

• Placing the finally block before a catch block is a syntax error.

• A try block does not require a finally block, sometimes no clean-up is needed.

• A try block can have no more than one finally block.

Page 14: Exception handling in ASP .NET

System.Exception

In .NET, only objects of class Exception and its derived classes may be thrown and caught.

Exceptions thrown in other .NET languages can be caught with the general catch clause.

Class Exception is the base class of .NET’s exception class hierarchy.

A catch block can use a base-class type to catch a hierarchy of related exceptions.

A catch block that specifies a parameter of type Exception can catch all exceptions.

Page 15: Exception handling in ASP .NET

Benefits of Exceptions

• The ability to keep code that deals with exceptional situations in a central place.

• The ability to locate and fix bugs in the code

• Unified error handling: all .NET Framework classes throw exceptions to handle error cases.

Page 16: Exception handling in ASP .NET

Benefits of Exceptions (conti.)

• Old Win32 APIs and COM returns a 32-bit error code. Exceptions include a string description of the problem.

• Exceptions also include a stack trace that tells you the path application took until the error occurred.

• You can also put any information you want in a user-defined exception of your own.

Page 17: Exception handling in ASP .NET

Example

OUTPUT:

Page 18: Exception handling in ASP .NET

• HelpLink:This is empty because it was not defined on the exception. HelpLink is a string property.

• Message:This is a short description of the exception's cause. Message is a read-only string property.

• Source:This is the application name. Source is a string property that can be assigned to or read from.

• StackTrace:This is the path through the compiled program's method hierarchy that the exception was generated from.

• TargetSite:This is the name of the method where the error occurred. This property helps simplify what part of the errors are recorded.

KEYWORDS

Page 19: Exception handling in ASP .NET
Page 20: Exception handling in ASP .NET

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 21: Exception handling in ASP .NET

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]