Download presentation
Presentation is loading. Please wait.
Published byGrace Goodman Modified over 8 years ago
1
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling
2
2 Handling Exception If without handling, Program crashes Falls into unknown state An exception handler is a section of program code that is designed to execute when a particular exception occurs Resolve the exception Lead to known state, such as exiting the program
3
Handling Exception Create application that can handle or resolve exception Enable clear, robust and more fault-tolerant programs 3
4
Keywords Try Include codes in which exceptions might occur Catch Represent types of exceptions the catch can handle Finally (Optional) codes present here will always execute Exception handling Process synchronous errors Follows the termination model of exception handling 4
5
Catch type Must be of class Exception or one that extends it directly or indirectly Stack unwinding CLR attempt to locate an enclosing try block in a calling method 5
6
The.Net Framework 6
7
System.Exception Properties e.Message the error message as a string; set in new Exception(msg); e.StackTrace trace of the method call stack as a string e.Source the application or object that threw the exception e.TargetSite the method object that threw the exception Methods e.ToString() returns the name of the exception 7
8
Throwing an Exception By an invalid operation (implicit exception) Division by 0 Index overflow Acess via a null reference By a throw statement (explicit exception) throw new FunnyException(10); class FunnyException : ApplicationException { public int errorCode; public FunnyException(int x) { errorCode = x; } } 8
9
Exception Hierarchy 9
10
Searching for a catch Clause Caller chain is traversed backwards until a method with a matching catch clause is found. If none is found => Program is aborted with a stack trace 10
11
Example: Handling Exception using try … catch block 11
12
12
13
13
14
Exception class’ Message and StackTrace Properties 14
15
15
16
Exception Message: this describes the exception 16
17
17
18
Catching Multiple Exceptions using multiple catch blocks 18
19
19
20
20
21
21
22
Important points about exception handling 22
23
The Finally Block 23
24
24
25
25
26
26
27
27
28
All the user defined exception should be derived from the ApplicationException class We will use this exception in method named Divide( ) 28
29
29 The code to define custom exception InvalidArgumentException is
30
Throwing an Exception: the throw keyword 30
31
31
32
Handling Overflows with Operators checked and unchecked Checked context The CLR throws an overflowException if overflow occur during calculation Unchecked context The result is of the overflow is truncated Explicit conversions between integral data types can cause overflow 32
33
using System; class Overflow { static void Main( string[] args ) { int number1 = Int32.MaxValue; // 2,147,483,647 int number2 = Int32.MaxValue; // 2,147,483,647 int sum = 0; Console.WriteLine( "number1: {0}\nnumber2: {1}", number1, number2 ); try { Console.WriteLine( "\nSum integers in checked context:" ); sum = checked( number1 + number2 ); } catch ( OverflowException overflowException ) { Console.WriteLine( overflowException.ToString() ); } Console.WriteLine( "\nsum after checked operation: {0}", sum ); Initialize and declare variables and assigned value to the maximum of int Sum adds number1 and number2 in a checked contextNumber1 and Number2 together causes an overflow, causes overflowException The catch handler gets the overflowException and prints out its string representation 33
34
Console.WriteLine( "\nSum integers in unchecked context:" ); sum = unchecked( number1 + number2 ); Console.WriteLine( "sum after unchecked operation: {0}", sum ); } // end method Main } // end class Overflow number1: 2147483647 number2: 2147483647 Sum integers in checked context: System.OverflowException: Arithmetic operation resulted in an overflow. at Overflow.Overflow.Main(String[] args) in f:\books\2001\csphtp1\csphtp1_examples\ch11\fig11_09\ overflow\overflow.cs:line 24 sum after checked operation: 0 Sum integers in unchecked context: sum after unchecked operation: -2 Addition of number1 and number2 in unchecked context Sum of the numbers in an unchecked context 34
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.