chapter 11: exception handling - e-tahtam.comturgaybilgin/2013-2014-bahar/se374_visual...microsoft...
TRANSCRIPT
![Page 1: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/1.jpg)
Chapter 11:
Exception Handling
![Page 2: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/2.jpg)
Understanding Exceptions
• Exception
– Any error condition or unexpected behavior in an executing program
• Exception handling
– Object-oriented techniques used to manage such errors
• Exceptions are objects of the Exception class or one of its derived classes
2Microsoft Visual C# 2012, Fifth Edition
![Page 3: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/3.jpg)
3Microsoft Visual C# 2012, Fifth Edition
![Page 4: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/4.jpg)
Purposely Generating a SystemException
• You can deliberately generate a SystemException by forcing a program to contain an error
– Example:• Dividing an integer by zero
• You don’t necessarily have to deal with exceptions
• Termination of the program is abrupt and unforgiving
• Object-oriented error-handling techniques provide more elegant solutions
4Microsoft Visual C# 2012, Fifth Edition
![Page 5: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/5.jpg)
Purposely Generating a SystemException(cont’d.)
5Microsoft Visual C# 2012, Fifth Edition
![Page 6: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/6.jpg)
Purposely Generating a SystemException(cont’d.)
6Microsoft Visual C# 2012, Fifth Edition
![Page 7: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/7.jpg)
Understanding Traditional and Object-Oriented Error-Handling Methods
• Check a variable’s value with an if statement before attempting to divide it into another number
– Prevents division by zero• However, it does not really “handle an exception”
– Is efficient if you think it will be a frequent problem• Has little “overhead”
• Otherwise, create an Exception object
7Microsoft Visual C# 2012, Fifth Edition
![Page 8: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/8.jpg)
Understanding Object-Oriented Exception-Handling Methods
• try block
– Contains statements that can produce an error
• Code at least one catch block or finally block immediately following a try block
• catch block
– Can “catch” one type of Exception
8Microsoft Visual C# 2012, Fifth Edition
![Page 9: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/9.jpg)
Understanding Object-Oriented Exception-Handling Methods (cont’d.)
9Microsoft Visual C# 2012, Fifth Edition
![Page 10: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/10.jpg)
10Microsoft Visual C# 2012, Fifth Edition
Understanding Object-Oriented Exception-Handling Methods (cont’d.)
![Page 11: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/11.jpg)
11Microsoft Visual C# 2012, Fifth Edition
Understanding Object-Oriented Exception-Handling Methods (cont’d.)
![Page 12: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/12.jpg)
Using the ExceptionClass’s ToString()Method and MessageProperty
• Using The Exception class and ToString()
– Provides a descriptive error message
– The user can receive precise information about the nature of any Exception that is thrown
12Microsoft Visual C# 2012, Fifth Edition
![Page 13: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/13.jpg)
Using the ExceptionClass’s ToString()Method and MessageProperty (cont’d.)
13Microsoft Visual C# 2012, Fifth Edition
![Page 14: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/14.jpg)
Using the ExceptionClass’s ToString()Method and MessageProperty (cont’d.)
14Microsoft Visual C# 2012, Fifth Edition
![Page 15: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/15.jpg)
Catching Multiple Exceptions
• You can place as many statements as you need within a try block
– Only the first error-generating statement throws an Exception
• Multiple catch blocks are examined in sequence until a match is found for the Exception that occurred
• Various Exceptions can be handled by the same catch block
15Microsoft Visual C# 2012, Fifth Edition
![Page 16: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/16.jpg)
Catching Multiple Exceptions (cont’d.)
16Microsoft Visual C# 2012, Fifth Edition
![Page 17: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/17.jpg)
17Microsoft Visual C# 2012, Fifth Edition
Catching Multiple Exceptions (cont’d.)
![Page 18: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/18.jpg)
18Microsoft Visual C# 2012, Fifth Edition
Catching Multiple Exceptions (cont’d.)
![Page 19: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/19.jpg)
19Microsoft Visual C# 2012, Fifth Edition
Catching Multiple Exceptions (cont’d.)
![Page 20: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/20.jpg)
20Microsoft Visual C# 2012, Fifth Edition
Catching Multiple Exceptions (cont’d.)
![Page 21: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/21.jpg)
• It is poor coding style for a method to throw more than three or four types of Exceptions
– The method is trying to accomplish too many diverse tasks
– The Exception types thrown are too specific and should be generalized
• Unreachable blocks
– Contain statements that can never execute under any circumstances because the program logic “can’t get there”
21Microsoft Visual C# 2012, Fifth Edition
Catching Multiple Exceptions (cont’d.)
![Page 22: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/22.jpg)
Using the finallyBlock
• finally block
– Contains actions to perform at the end of a try…catchsequence
– Executes whether the try block identifies any Exceptions or not
– Used to perform clean-up tasks
• A finally block executes after:– The try ends normally
– The catch executes
– The try ends abnormally and the catch does not execute
22Microsoft Visual C# 2012, Fifth Edition
![Page 23: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/23.jpg)
Using the finallyBlock (cont’d.)
23Microsoft Visual C# 2012, Fifth Edition
![Page 24: Chapter 11: Exception Handling - e-tahtam.comturgaybilgin/2013-2014-bahar/SE374_Visual...Microsoft Visual C# 2012, Fifth Edition 10 Understanding Object-Oriented Exception-Handling](https://reader034.vdocuments.us/reader034/viewer/2022051321/5b27dcf07f8b9a104d8b4e53/html5/thumbnails/24.jpg)
Using the finallyBlock (cont’d.)
24Microsoft Visual C# 2012, Fifth Edition