Download presentation
Presentation is loading. Please wait.
1
Exceptions C++ Interlude 3
Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
2
Contents Background Assertions Throwing Exceptions Handling Exceptions
Programmer-Defined Exception Classes Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
3
Definition An exception is an object We handle the exception Example:
Signals the rest of the program Something unexpected has happened. We handle the exception When we detect and react to it. Example: Need a consistent mechanism to let client know a method could not perform task Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
4
Problem to Solve Consider the video game that we are working on for our friend Next task – create function that searches for given string in a number of boxes First try at the function findBox Meets the basic requirements: Searches array of boxes and returns box containing target string. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
5
Function findBox First try at the function findBox
Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
6
But may not be best solution
Function findBox But may not be best solution Revised findBox function with assertions Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
7
Throwing Exceptions Alternate way of communicating or returning information to function’s client is throw an exception Bypasses normal execution, Control immediately returns to client. Exception can contain information about error or unusual condition Helps the client resolve the issue and possibly try the function again Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
8
Throwing Exceptions Hierarchy of C++ exception classes
Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
9
Throwing Exceptions Revised findBox function throws exception
Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
10
When to Throw Exception
If you can resolve unusual situation in a reasonable manner Do so without exception Several resolutions to abnormal occurrence, you want client to choose Throw an exception. Detect programmer used method incorrectly Throw runtime exception. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
11
Handling Exceptions Code that consists of two pieces.
Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
12
Handling Exceptions Statements in try block execute as if block not present If no exception, catch block ignored If exception occurs of type specified in catch block Remaining part of try block ignored Program jumps to statements in catch block Program then continues to statements after the catch block Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
13
Handling Exceptions Trying the function findBox
Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
14
Handling Exceptions Trying the function findBox
Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
15
Multiple catch Blocks Catch blocks must be ordered so most specific exception classes caught before more general exception classes Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
16
Uncaught Exceptions A program with an uncaught exception
Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
17
Uncaught Exceptions A program with an uncaught exception
Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
18
Uncaught Exceptions Flow of control for an uncaught exception
Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
19
Uncaught Exceptions Flow of control for an uncaught exception
Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
20
Programmer-Defined Exception Classes
Exception class typically consists of a constructor that has a string parameter. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
21
To Throw An Exception Determine what errors to check, where they can occur Detect error, usually with if statement If error occurs, use throw statement Pass string description parameter Add throw clause to function header indicating exceptions Derive custom exception class to better identify conditions (optional) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
22
To Handle An Exception Place statement that might throw an exception in try block After try block, place catch blocks for each type of exception that can be thrown by statements in try block. Place the catch blocks in order from most specific exception classes to more general Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.