Академический Документы
Профессиональный Документы
Культура Документы
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
13
Using the Exception Class’s ToString()
Method and Message Field
15
Catching Multiple Exceptions
18
Catching Multiple Exceptions
19
Catching Multiple Exceptions
20
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
29
Tracing Exceptions Through the Call Stack
30
Creating Your Own Exception Classes
31
Chapter Summary
32
Chapter Summary
33
Chapter Summary
34