Professional Documents
Culture Documents
Lecture 14 - Exception Handling
Lecture 14 - Exception Handling
Exception handling
Concordia University
11/21/2022 2
What we learn in Lecture 14
• Exception handling
• Try and catch block
11/21/2022 3
Introduction
• Exception
– Undesirable event detectable during program execution
• Code to handle exceptions depends on the type of application
being developed
– May or may not want the program to terminate when an
exception occurs
• Can add exception-handling code at point where an error can
occur
4
Exception Handling in C++
5
try/catch Block template
6
try Block
Statements that may generate an exception
are placed in a try block.
try
{
// statements
Exception must be thrown in a try block
throw “exception”
using keyword throw.
// statements
}
- The try block also contains statements that
should not be executed if an exception occurs.
- If an exception is thrown in a try block, the
remaining statements (in try block) are ignored
7
catch Block Specifies the type of exception it can catch
If the heading of a catch block has an ellipsis (three dots) in place of parameters
• This block can catch exceptions of all types (e.g., int, double, object)
8
catch Block (cont.)
catch (…){
catch (int) {}
9
Multiple Catch Blocks
try
{
// statements
throw “exception”
// statements
}
catch (type exception)
{
// exception-handling code
}
catch (type exception)
{
// exception-handling code
}
⋮
• Program searches catch blocks in the order they appear after the try
block and looks for an appropriate exception handler.
• If the type of thrown exception matches the parameter type in one of the
catch blocks:
• Code of that catch block executes
• Remaining catch blocks are ignored
10
try/catch Block (cont.)
• For try/catch to work, the exception must be thrown in the try block
• General syntax:
throw exception;
– expression is a constant value, variable, or object
• Object being thrown can be a specific object or an anonymous object
• In C++, an exception is a value
11
Example: try/catch Block
12
Example: try/catch Block
13
Example: try/catch Block
Normal case
14
Example: try/catch Block
Exceptional case
15
Example 2: try/catch Block (cont.)
16
Example 2: try/catch Block (cont.)
17
try/catch Block (cont.)
18
Example: Multiple Catch Blocks
• try block is followed by one or more catch blocks
19
Example: Multiple Catch Blocks (cont.)
• try block is followed by one or more catch blocks
20
Example: Multiple Catch Blocks (cont.)
• try block is followed by one or more catch blocks
21
Catch any Throw
• We can also define a catch block that captures all the exceptions
independently of the type used in the call to throw
• In this case, we have to write an ellipsis (three dots) in place of
parameters in the heading of a catch block
try
{
// statements
}
catch (...)
{
cout << "Exception occurred";
}
22
Multiple exception handled by one
catch block
23
Order of catch Blocks
24
Catching Exceptions
- when an exception is thrown, it can be caught and handled in the try-
catch block as follows:
try {
statement1;
statement2;
statement3;
}
catch (type1 exVar1) {
handler for exception1;
}
catch (type2 exVar2) {
handler for exception2;
}
...
catch (…) {
handler for any exception;
}
Next statement;
25
Trace Catching Exceptions
- when an exception is thrown, it can be caught and handled in the try-
catch block as follows:
try {
Suppose an exception of type1 is
statement1;
thrown in statement2
statement2;
statement3;
}
catch (type1 exVar1) {
handler for exception1;
}
catch (type2 exVar2) {
handler for exception2;
}
...
catch (…) {
handler for any exception;
}
Next statement;
26
Trace Catching Exceptions
- when an exception is thrown, it can be caught and handled in the try-
catch block as follows:
try {
statement1;
statement2;
statement3;
} The exception is handled in this
catch (type1 exVar1) { catch block.
handler for exception1;
}
catch (type2 exVar2) {
handler for exception2;
}
...
catch (…) {
handler for any exception;
}
Next statement;
27
Trace Catching Exceptions
- when an exception is thrown, it can be caught and handled in the try-
catch block as follows:
try {
statement1;
statement2;
statement3;
}
catch (type1 exVar1) {
handler for exception1;
}
catch (type2 exVar2) {
handler for exception2;
}
...
catch (…) {
handler for any exception;
The Next statement following all
}
catch blocks is always executed.
Next statement;
28
Trace Catching Exceptions
- when an exception is thrown, it can be caught and handled in the try-
catch block as follows:
try {
statement1; Suppose an exception other than type 1
statement2; and type 2 is thrown in statement2
statement3;
}
catch (type1 exVar1) {
handler for exception1;
}
catch (type2 exVar2) {
handler for exception2;
}
...
catch (…) {
handler for any exception;
}
Next statement;
29
Trace Catching Exceptions
- when an exception is thrown, it can be caught and handled in the try-
catch block as follows:
try {
statement1;
statement2;
statement3;
}
catch (type1 exVar1) {
handler for exception1;
}
catch (type2 exVar2) {
handler for exception2;
}
The exception is handled in this
...
catch block.
catch (…) {
handler for any exception;
}
Next statement;
30
Trace Catching Exceptions
- when an exception is thrown, it can be caught and handled in the try-
catch block as follows:
try {
statement1;
statement2;
statement3;
}
catch (type1 exVar1) {
handler for exception1;
}
catch (type2 exVar2) {
handler for exception2;
}
...
catch (…) {
handler for any exception;
The Next statement following all
}
catch blocks is always executed.
Next statement;
31
Order of catch blocks
32
Exception handling and Functions
• Exception handling enables the caller of the function to process the exception thrown from
a function.
• The callee detects the exception and throws it.
• The caller catches it and handles it.
33
Another example of try/catch
34
Exercise
• Can you throw multiple exceptions in one throw statement?
• Can you have multiple catch blocks in a try-catch block?
11/21/2022 35