Chapter 04 Exception Handling
Chapter 04 Exception Handling
Exception Handling
1
Objectives
2
Objectives
3
Objectives
4
Understanding Exceptions
5
Understanding Exceptions
6
Purposely Generating a SystemException
7
Purposely Generating a SystemException
8
Understanding Object-Oriented Exception-
Handling Methods
• In object-oriented terminology, you “try” a procedure that
may not complete correctly
• A method that detects an error condition or Exception
“throws” an Exception
• The block of code that processes the error “catches” the
Exception
9
Understanding Object-Oriented Exception-
Handling Methods
• When you write a block of code in which something
can go wrong, you can place the code in a try block,
consisting of:
– The keyword try
– An opening curly brace
– Statements that might cause Exceptions
– A closing curly brace
10
Understanding Object-Oriented Exception-
Handling Methods
• You create a catch block with the following elements:
– The keyword catch, followed by an opening parenthesis, the
Exception type, a name for an instance of the Exception type, and a
closing parenthesis
– An opening curly brace
– Statements that take the action you want to use to deal with the error
condition
– A closing curly brace
11
Understanding Object-Oriented Exception-
Handling Methods
12
Using the Exception Class’s ToString() Method
and Message Field
• Any Exception generated from within a try block would be
caught by a catch block whose argument is an Exception type
(e.g., Exception e); there is no way to confirm the origin of the
Exception
• You can use the ToString() method to provide a descriptive
error message
• The Exception class also contains a field named Message that
contains useful information about an Exception
13
Using the Exception Class’s ToString() Method
and Message Field
14
Catching Multiple Exceptions
15
Catching Multiple Exceptions
16
Catching Multiple Exceptions
17
Catching Multiple Exceptions
18
Catching Multiple Exceptions
19
Catching Multiple Exceptions
• The Exception class is the base class for all Exception objects
and therefore can reference all Exception descendants
• The catch block in the previous code accepts all Exception
argument types
• When you list multiple catch blocks following a try, you must
be careful that some catch blocks don’t become unreachable
20
Catching Multiple Exceptions
21
Catching Multiple Exceptions
22
Using the finally Block
23
Using the finally Block
24
Handling an Exception with a Loop
25
Throwing Exceptions
26
Throwing Exceptions
27
Throwing Exceptions
28
Tracing Exceptions Through the Call Stack
29
Tracing Exceptions Through the Call Stack
30
Creating Your Own Exception Classes
• You can create your own customized Exception class for your
application
• To create your own Exception that you can throw, you should
extend the ApplicationException class
• You should not create an excessive number of special
Exception types for your class because it adds a level of
complexity to your program
31
Chapter Summary
32
Chapter Summary
33
Chapter Summary
34