You are on page 1of 35

Lecture 14:

Exception handling

Dr. Fadi Alzhouri

Concordia University

Created by Rose Gomar, presented by Fadi Alzhouri


Textbook (Copyright)

“C++ How to Program”, 10th edition, by Harvey and


Paul Deitel Print ISBN: 0-13-444823-5, Chapter 17.

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++

• Four mechanisms to handle exceptions within a


program
– Use the predefined function assert
– Use the try-catch block
o Use primitive types.
o Use the C++ built-in exception classes
o Create your own exception classes

5
try/catch Block template

• A template for a try-throw-catch block may look like this:


try
{
// statements
throw “exception”
// statements
}

catch (type exception)


{
// exception-handling code
}

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

catch (type exception)


{
Contains an exception handler
// exception-handling code
}


If no exception is thrown in a try block
• All catch blocks are ignored
• Execution resumes after the last catch
block

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 block (cont.)


• Can have at most one catch block parameter
• catch block parameter becomes a placeholder for the value thrown (i.e.,
thrown value stored in the catch block parameter)

catch (int x, int y){

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

When num2 is zero, the try


block throws an exception
to the catch block.

12
Example: try/catch Block

the exception is caught


by the catch block.

13
Example: try/catch Block
Normal case

14
Example: try/catch Block
Exceptional case

15
Example 2: try/catch Block (cont.)

Output: Division by zero condition

16
Example 2: try/catch Block (cont.)

17
try/catch Block (cont.)

• In the previous example, if within the n loop, n is evaluated to be


greater than 9 an exception is thrown
• When throw is executed, the remaining statements in try
block are ignored and every object created within try block is
destroyed
• Then, control is passed to the corresponding catch block
• The program execution resumes after the catch block (in this
case: return 0;)

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

• Catch block can catch:


• All exceptions of a specific type
• All types of exceptions
• A catch block with an ellipsis (. . .) catches any type of exception
• If used, it should be the last catch block of that sequence
• Be careful about the order in which you list 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.

int fun(parameters){ The callee throws


statement4;
throw exception;
statement5;
}
int main() {
statement1;
Try{
statement2;
fun(arguments);
statement3;
The caller catches it
}
catch (…){
handler for any exception;
}

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

You might also like