SlideShare a Scribd company logo
6
Most read
7
Most read
8
Most read
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 Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
Nithil.pp
nithilp@gmail.com
facebook.com/ nithil
padinare peediyekal
twitter.com/nithilpp
in.linkedin.com/in/nithil.pp
9995223505
Exception Handling in .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.
What is Exception? (cont.)
• Simplify code construction and maintenance
• Allow the problematic situations to be
processed at multiple levels
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
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
}
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
Common .NET Exceptions
 The 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.
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.
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.
finally block
C# 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.
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.
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.
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.
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.
Example
OUTPUT:
• 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
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
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

More Related Content

What's hot (20)

PPTX
Polymorphism presentation in java
Ahsan Raja
 
PPT
Abstract class
Tony Nguyen
 
PPTX
VB.NET:An introduction to Namespaces in .NET framework
Richa Handa
 
PPTX
Operators in java
Then Murugeshwari
 
PPT
Object-oriented concepts
BG Java EE Course
 
PPTX
Classes and Objects in C#
Adeel Rasheed
 
PPT
Exception handling in java
Pratik Soares
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PPTX
interface in c#
Deepti Pillai
 
PPTX
Interface in java
PhD Research Scholar
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
PPTX
Servlets
ZainabNoorGul
 
ODP
OOP java
xball977
 
PPTX
Exception handling
PhD Research Scholar
 
PPTX
Object Oriented Programming Concepts for beginners
Vibhawa Nirmal
 
PPT
Uml class-diagram
ASHOK KUMAR PALAKI
 
Polymorphism presentation in java
Ahsan Raja
 
Abstract class
Tony Nguyen
 
VB.NET:An introduction to Namespaces in .NET framework
Richa Handa
 
Operators in java
Then Murugeshwari
 
Object-oriented concepts
BG Java EE Course
 
Classes and Objects in C#
Adeel Rasheed
 
Exception handling in java
Pratik Soares
 
Classes objects in java
Madishetty Prathibha
 
interface in c#
Deepti Pillai
 
Interface in java
PhD Research Scholar
 
Exception Handling in JAVA
SURIT DATTA
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Servlets
ZainabNoorGul
 
OOP java
xball977
 
Exception handling
PhD Research Scholar
 
Object Oriented Programming Concepts for beginners
Vibhawa Nirmal
 
Uml class-diagram
ASHOK KUMAR PALAKI
 

Similar to Exception handling in ASP .NET (20)

PPTX
Exception handling in .net
baabtra.com - No. 1 supplier of quality freshers
 
PPT
Exceptions
motthu18
 
PPTX
12. Exception Handling
Intro C# Book
 
PPTX
Exceptions overview
Bharath K
 
PPTX
3.C#
Raghu nath
 
PPT
Exception
abhay singh
 
PPT
Exceptions
DeepikaT13
 
PPTX
Exception Handlin g C#.pptx
R S Anu Prabha
 
PPTX
IakakkakjabbhjajjjjjajjajwsjException.pptx
KingDietherManay1
 
PPTX
C Sharp Tutorial : C Sharp Exception
Courseing Online
 
PPT
12 Exceptions handling
maznabili
 
PPTX
Presentation1
Anul Chaudhary
 
PPTX
Exception Handling in C#
Abid Kohistani
 
PPTX
Role of .NET in Exception Handling
Asrarulhaq Maktedar
 
PPTX
Chapter_4_WP_with_C#_Exception_Handling_student_1.0.pptx
ZachariahAbera
 
PPTX
6-Error Handling.pptx
amiralicomsats3
 
PPTX
Exception guidelines in c#
Ahmed Helmy
 
PPTX
43c
Sireesh K
 
PPTX
41c
Sireesh K
 
PPT
Understanding Exception Handling in .Net
Mindfire Solutions
 
Exceptions
motthu18
 
12. Exception Handling
Intro C# Book
 
Exceptions overview
Bharath K
 
Exception
abhay singh
 
Exceptions
DeepikaT13
 
Exception Handlin g C#.pptx
R S Anu Prabha
 
IakakkakjabbhjajjjjjajjajwsjException.pptx
KingDietherManay1
 
C Sharp Tutorial : C Sharp Exception
Courseing Online
 
12 Exceptions handling
maznabili
 
Presentation1
Anul Chaudhary
 
Exception Handling in C#
Abid Kohistani
 
Role of .NET in Exception Handling
Asrarulhaq Maktedar
 
Chapter_4_WP_with_C#_Exception_Handling_student_1.0.pptx
ZachariahAbera
 
6-Error Handling.pptx
amiralicomsats3
 
Exception guidelines in c#
Ahmed Helmy
 
Understanding Exception Handling in .Net
Mindfire Solutions
 
Ad

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
PDF
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 3 stored procedures
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Ad

Recently uploaded (20)

PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Introduction presentation of the patentbutler tool
MIPLM
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
infertility, types,causes, impact, and management
Ritu480198
 
epi editorial commitee meeting presentation
MIPLM
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 

Exception handling in ASP .NET

  • 2. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 4. 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.
  • 5. What is Exception? (cont.) • Simplify code construction and maintenance • Allow the problematic situations to be processed at multiple levels
  • 6. 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
  • 7. 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 }
  • 8. 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
  • 9. Common .NET Exceptions  The 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.
  • 10. 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.
  • 11. 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.
  • 12. finally block C# 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.
  • 13. 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.
  • 14. 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.
  • 15. 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.
  • 16. 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.
  • 18. • 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
  • 20. 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
  • 21. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: [email protected]