Exception Handling in C#
Exception Handling in C#
Exception Handling in C#
CHAPTER 5
C#
EXCEPTION HANDLING
1. Introduction
2. Exception Handling
3. Exception classes in C#
4. User defined Exception
5. Benefits of Exceptions
01/24/2021
Introduction
3
01/24/2021
Exception
4
An Exception
It is a problem that arises during the execution of a
program.
A C# exception is a response to an exceptional
situation that arises while a program is running, such
as an attempt to divide by zero.
Exceptions provide a way to transfer control
from one part of a program to another
01/24/2021
5
01/24/2021
How Do Exceptions Handling Work?
7
01/24/2021
8
01/24/2021
9
01/24/2021
10
01/24/2021
11
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.
01/24/2021
12
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 parenthesis 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.
01/24/2021
13
Once the catch block that matches the exception is found, you
have 3 choices:
1. Re-throw the same exception, notifying the higher-up call stack of
the exception
2. Throw a different exception, giving richer exception information
to code higher-up in the call stack
3. 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.
01/24/2021
14
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.
01/24/2021
15
01/24/2021
Syntax
16
01/24/2021
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
17
// within the try block. This code ALWAYS executes.
01/24/2021
}
Exception Classes in C#
18
01/24/2021
19
01/24/2021
20
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.
01/24/2021
Example 1:
23
01/24/2021
Example 2:
24
01/24/2021
Choosing the Exception to throw
25
try {
// code that might fail…
}
catch (Exception) {
…
}
How can you write code that can recover from all
situations???
A class library should never ever swallow all exceptions. The
application should get a chance to handle the exception.
You can catch all exceptions only if you are going to process
it and re-throw it again.
01/24/2021
Array in C#
27
Declaring an Array
int [] my_array= new int[size];
Array initialization
int [] my_array= {1,2,3,4};
Access to the elements of an Array
We access the array elements directly using their
indices. (0 to size-1)
My_array[index]=100;
01/24/2021
Exercise 1:
28
Use
IndexOutOfRangeException
&
FormatException
&
Exception
01/24/2021
User Defined Exceptions
29
01/24/2021
Example 2:
30
if(value<0)
throw new NegativeNumberException(" Use Only Positive
numbers");
01/24/2021
Benefits of Exceptions
31
01/24/2021
32
01/24/2021
Exercise 3:
33
01/24/2021
Exercise 4:
34
01/24/2021
Exercise 5: Order of Exception
35
(A) (B)
01/24/2021
Exercise 7: Adding Exceptions-2
37
01/24/2021
Exercise 8: FormatExceptions
38
01/24/2021
Exercise 9: User defined Exception-1
39
01/24/2021
Exercise 10: User defined Exception-2
40
01/24/2021
41
Questions?
01/24/2021
42
Thank You
01/24/2021