SlideShare a Scribd company logo
Exception Handling
Exception Handling in .net
 Exceptions are types that all ultimately derive
from System.Exception.
 Use a try block around the statements that might throw
exceptions.
 Once an exception occurs in the try block, the flow of
control jumps to the first associated exception handler
that is present anywhere in the call stack. In C#,
the catch keyword is used to define an exception
handler.
 If no exception handler for a given exception is present,
the program stops executing with an error message.
Syntax
try {
// code that requires common cleanup or
// exception-recovery operations
}
catch (InvalidOperationException) {
//code that recovers from an InvalidOperationException
// (or any exception type derived from it)
}
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. This code ALWAYS executes.
}
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.
 You may or may not specify a catch type in parantheses after catch :
 The catch type must be of type System.Exception or a type that derived
from System.Exception
 If there is no catch type specified, that catch block handles any exception.
This is equivalent to having a catch block that specifies System.Exception
as a catch type.
 CLR searches for a matching catch type from top to bottom.
 If CLR cannot find any catch type that matches the exception, CLR
continues searching up the callstack to find a catch type.
 Once the catch block that matches the exception is found,
you have 3 choices:
 Re-throw the same exception
 Throw a different exception, giving richer exception
information to code.
 Let the code continue from the bottom of the catch block
 In choices 1-2, an exception is thrown and code starts
looking for a catch block whose type matches the
exception thrown
 In choice 3, the finally block is executed
 You can also specify a variable name like
catch(Exception e) to access information specific to the
exception.
finally block
 GC does automatic memory clean-up, it only cleans up if there are no
references kept on the object.
 Thus, memory leaks can occur if programmers carelessly keep references to
unwanted objects.
 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.
 Exception Classes in C#
 C# exceptions are represented by classes. The exception
classes in C# are mainly directly or indirectly derived from
the System.Exception class. Some of the exception classes
derived from the System.Exception class are
the System.ApplicationException and System.SystemE
xception classes.
 The System.ApplicationException class supports
exceptions generated by application programs. So the
exceptions defined by the programmers should derive from
this class.
 The System.SystemException class is the base class for all
predefined system exception.
System.Exception Properties
 Class Exception’s properties are used to formulate error messages
indicating a caught exception.
 Property Message stores the error message associated with an Exception
object.
 Property StackTrace contains a string that represents the method-call
stack.
 When an exception occurs, a programmer might use a different error
message or indicate a new exception type.
 The original exception object is stored in the InnerException property.
 Other properties:
 HelpLink specifies the location of a help file that describes the problem.
 Source specifies the name of the application or object that caused the
exception.
 TargetSite specifies the method where the exception originated.
Defined Exception
Exception Class Description
System.IO.IOException Handles I/O errors.
System.IndexOutOfRangeException Handles errors generated when a method refers to an array index
out of range.
System.ArrayTypeMismatchException Handles errors generated when type is mismatched with the array
type.
System.NullReferenceException Handles errors generated from deferencing a null object.
System.DivideByZeroException Handles errors generated from dividing a dividend with zero.
System.InvalidCastException Handles errors generated during typecasting.
System.OutOfMemoryException Handles errors generated from insufficient free memory.
System.StackOverflowException Handles errors generated from stack overflow.
Questions ??

More Related Content

PPT
Exceptions
PPT
Exceptions
PPT
9781439035665 ppt ch11
PPT
Chap12
PPTX
Exception Handling in Java
PPT
Exception handling
PPT
Exception Handling
PPTX
JAVA - Throwable class
Exceptions
Exceptions
9781439035665 ppt ch11
Chap12
Exception Handling in Java
Exception handling
Exception Handling
JAVA - Throwable class

What's hot (20)

PPTX
Exceptions overview
PPTX
Exception handling in c
PPTX
exception handling in java
DOCX
Exceptions handling notes in JAVA
ODP
Exception Handling In Java
PPTX
Java exception handling
PDF
Java review: try catch
PPTX
Exception handling in java
PPTX
Z blue exception
PDF
Java unit 11
PPTX
17 exceptions
PPTX
Exception handling in java
PPTX
130410107010 exception handling
PPT
Exception handling
PPTX
Interface andexceptions
PPTX
Exception handling in java
PPT
12 exception handling
PPTX
Exception handling in java
PPTX
Presentation on-exception-handling
Exceptions overview
Exception handling in c
exception handling in java
Exceptions handling notes in JAVA
Exception Handling In Java
Java exception handling
Java review: try catch
Exception handling in java
Z blue exception
Java unit 11
17 exceptions
Exception handling in java
130410107010 exception handling
Exception handling
Interface andexceptions
Exception handling in java
12 exception handling
Exception handling in java
Presentation on-exception-handling
Ad

Viewers also liked (8)

PPT
Csphtp1 23
PPT
Csphtp1 10
PPT
Csphtp1 05
PPT
Csphtp1 15
PPT
Csphtp1 08
PPT
Csphtp1 18
PPT
Apparel 2 chap 08
PDF
Web2.0 guide by_twlog
Csphtp1 23
Csphtp1 10
Csphtp1 05
Csphtp1 15
Csphtp1 08
Csphtp1 18
Apparel 2 chap 08
Web2.0 guide by_twlog
Ad

Similar to 3.C# (20)

PPTX
PPTX
IakakkakjabbhjajjjjjajjajwsjException.pptx
PPTX
Exception handling in ASP .NET
PPTX
Exception Handling in C#
PPTX
C Sharp Tutorial : C Sharp Exception
PPTX
12. Exception Handling
PPTX
Presentation1
PPT
Exception
PPTX
Exception Handlin g C#.pptx
PPTX
6-Error Handling.pptx
PPTX
PPTX
PPTX
Chapter_4_WP_with_C#_Exception_Handling_student_1.0.pptx
PPT
12 Exceptions handling
PPT
C# Exceptions Handling
PDF
Internals of exceptions
PPTX
Exception guidelines in c#
PPT
Exception Handling Mechanism in .NET CLR
PPTX
Role of .NET in Exception Handling
PPT
Exception handling
IakakkakjabbhjajjjjjajjajwsjException.pptx
Exception handling in ASP .NET
Exception Handling in C#
C Sharp Tutorial : C Sharp Exception
12. Exception Handling
Presentation1
Exception
Exception Handlin g C#.pptx
6-Error Handling.pptx
Chapter_4_WP_with_C#_Exception_Handling_student_1.0.pptx
12 Exceptions handling
C# Exceptions Handling
Internals of exceptions
Exception guidelines in c#
Exception Handling Mechanism in .NET CLR
Role of .NET in Exception Handling
Exception handling

More from Raghu nath (20)

PPTX
Mongo db
PDF
Ftp (file transfer protocol)
PDF
MS WORD 2013
PDF
Msword
PDF
Ms word
PDF
Javascript part1
PDF
Regular expressions
PDF
Selection sort
PPTX
Binary search
PPTX
JSON(JavaScript Object Notation)
PDF
Stemming algorithms
PPTX
Step by step guide to install dhcp role
PPTX
Network essentials chapter 4
PPTX
Network essentials chapter 3
PPTX
Network essentials chapter 2
PPTX
Network essentials - chapter 1
PPTX
Python chapter 2
PPTX
python chapter 1
PPTX
Linux Shell Scripting
PPTX
Mongo db
Ftp (file transfer protocol)
MS WORD 2013
Msword
Ms word
Javascript part1
Regular expressions
Selection sort
Binary search
JSON(JavaScript Object Notation)
Stemming algorithms
Step by step guide to install dhcp role
Network essentials chapter 4
Network essentials chapter 3
Network essentials chapter 2
Network essentials - chapter 1
Python chapter 2
python chapter 1
Linux Shell Scripting

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Spectroscopy.pptx food analysis technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Empathic Computing: Creating Shared Understanding
NewMind AI Weekly Chronicles - August'25 Week I
Chapter 3 Spatial Domain Image Processing.pdf
Programs and apps: productivity, graphics, security and other tools
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Spectroscopy.pptx food analysis technology
Machine learning based COVID-19 study performance prediction
Review of recent advances in non-invasive hemoglobin estimation
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Diabetes mellitus diagnosis method based random forest with bat algorithm
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Mobile App Security Testing_ A Comprehensive Guide.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Understanding_Digital_Forensics_Presentation.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11

3.C#

  • 2. Exception Handling in .net  Exceptions are types that all ultimately derive from System.Exception.  Use a try block around the statements that might throw exceptions.  Once an exception occurs in the try block, the flow of control jumps to the first associated exception handler that is present anywhere in the call stack. In C#, the catch keyword is used to define an exception handler.  If no exception handler for a given exception is present, the program stops executing with an error message.
  • 3. Syntax try { // code that requires common cleanup or // exception-recovery operations } catch (InvalidOperationException) { //code that recovers from an InvalidOperationException // (or any exception type derived from it) } 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. This code ALWAYS executes. }
  • 4. 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.
  • 5. 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.  You may or may not specify a catch type in parantheses after catch :  The catch type must be of type System.Exception or a type that derived from System.Exception  If there is no catch type specified, that catch block handles any exception. This is equivalent to having a catch block that specifies System.Exception as a catch type.  CLR searches for a matching catch type from top to bottom.  If CLR cannot find any catch type that matches the exception, CLR continues searching up the callstack to find a catch type.
  • 6.  Once the catch block that matches the exception is found, you have 3 choices:  Re-throw the same exception  Throw a different exception, giving richer exception information to code.  Let the code continue from the bottom of the catch block  In choices 1-2, an exception is thrown and code starts looking for a catch block whose type matches the exception thrown  In choice 3, the finally block is executed  You can also specify a variable name like catch(Exception e) to access information specific to the exception.
  • 7. finally block  GC does automatic memory clean-up, it only cleans up if there are no references kept on the object.  Thus, memory leaks can occur if programmers carelessly keep references to unwanted objects.  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.
  • 8.  Exception Classes in C#  C# exceptions are represented by classes. The exception classes in C# are mainly directly or indirectly derived from the System.Exception class. Some of the exception classes derived from the System.Exception class are the System.ApplicationException and System.SystemE xception classes.  The System.ApplicationException class supports exceptions generated by application programs. So the exceptions defined by the programmers should derive from this class.  The System.SystemException class is the base class for all predefined system exception.
  • 9. System.Exception Properties  Class Exception’s properties are used to formulate error messages indicating a caught exception.  Property Message stores the error message associated with an Exception object.  Property StackTrace contains a string that represents the method-call stack.  When an exception occurs, a programmer might use a different error message or indicate a new exception type.  The original exception object is stored in the InnerException property.  Other properties:  HelpLink specifies the location of a help file that describes the problem.  Source specifies the name of the application or object that caused the exception.  TargetSite specifies the method where the exception originated.
  • 10. Defined Exception Exception Class Description System.IO.IOException Handles I/O errors. System.IndexOutOfRangeException Handles errors generated when a method refers to an array index out of range. System.ArrayTypeMismatchException Handles errors generated when type is mismatched with the array type. System.NullReferenceException Handles errors generated from deferencing a null object. System.DivideByZeroException Handles errors generated from dividing a dividend with zero. System.InvalidCastException Handles errors generated during typecasting. System.OutOfMemoryException Handles errors generated from insufficient free memory. System.StackOverflowException Handles errors generated from stack overflow.