Module 7: Exception Handling By SRIRAM . B
Overview
Exceptions
Exception Handling Keywords
Exception Types (System Defined, User Defined)
Using try and catch
Using try and finally
Handling try-catch-finally block
Throwing Exceptions
Exceptions
An exception is an erroneous situation that occurs during program execution.
Exception-handling provides a structured and uniform way of handling system-
All exceptions are represented by an instance of a predefined class,
level and application-level errors.
System.Exception.
Some of the system level exceptions are :
A division by zero A reference to a Null value
Exception Handling Keywords
Try
Catch
finally
System Defined Exceptions
Exception (Base Class)
IndexOutOfRangeException
NullReferenceException
ArgumentException
ArrayTypeMismatchException
OverflowException
ArithmeticException
DivideByZeroException
InvalidOperationException
User Defined Exceptions
Besides the default or system exceptions, a programmer can also creates his own set of exceptions. The advantage is :-
It can hold more information about the error condition than a standard class.
The user can tailor the catch blocks to the specific exception type, instead of catching a generic exception.
Try Block A try block guards the statements that may throw an exception. try { //statements that may cause an exception
}
Using try and catch Block You can associate an exception-handler with the try block by providing one or more catch-handlers immediately after the try block. try { //statements that may cause an exception } catch (…) { //error handling code }
Using try and finally block The finally block is used to execute a given set of statements, irrespective of whether an exception is thrown or not. try { //statements that may cause an exception } finally { //statements to be executed }
Handling try-catch-finally block
Since catch performs exception handling and finally performs the cleanup, the best approach is to merge both applications.
Syntax :try { //statements that may cause an exception } catch (…) { //error handling code } finally { //statements to be executed }
Finally block
If no exception is thrown from the try block, control passes to the finally block after the closing brace of the try block.
If no catch block is specified or none of the catch blocks match the exception.
If a matching catch block exists, it gets executed, after which the catch block control comes to finally block.
Throwing Exceptions
To throw an exception explicitly one has to use the throw statement.
Helpful for throwing user defined exceptions
Example 1 – Exception Handling using System; namespace Exception Handling { class Add_nums { public static void add(int a, int b) { int res=a/b; Console.WriteLine(res); } public static void Main() { Add_nums.add(10,0); } } Result in Exception
Example 2 – Exception Handling Using System; namespace Exception Handling { public class excep1 { static void Main(string[] arg) { int[] a ={ 2, 4, 12, 56, 23 }; for (int i = 0; i <= 5; i++) try { Console.WriteLine("The {0}th element of the array is {1}", i, a[i]); } catch (IndexOutOfRangeException e)
Example 2 – Exception Handling.. { Console.WriteLine("An Exception has occured"); Console.WriteLine("The description is } } } }
" + e);
Exception Handling Flashback
Exceptions
Exception Handling Keywords
Exception Types (System Defined, User Defined)
Using try and catch
Using try and finally
Handling try-catch-finally block
Throwing Exceptions
Session Ends
Exercise
Relax