Download presentation
Presentation is loading. Please wait.
Published byHelena Lewis Modified over 6 years ago
1
Object-Oriented Programming Using C++ Second Edition
12 Handling Exceptions Object-Oriented Programming Using C++ Second Edition This lecture is for interests not for the exam.
2
12 Objectives In this lecture, you will learn:
About the limitations of traditional error-handling methods How to throw exceptions How to use try blocks How to catch exceptions How to use multiple throw statements and multiple catch blocks How to throw objects How to use the default exception handler How to use exception specifications About unwinding the stack How to handle memory allocation exceptions
3
Understanding the Limitations of Traditional Error Handling
12 Many C++ programs contain code similar to the code in Figure 12-1 The program uses the exit() function, which forces the program to end When you use the exit() function, the program ends abruptly If the program is a spreadsheet or a game, the user might become annoyed that the program has prematurely stopped working
4
Understanding the Limitations of Traditional Error Handling
12 Ex12-1 A slightly better alternative to exit() involves using the atexit() function The atexit() function requires a function name as its argument; this function is then registered with atexit(), which means the named function is called automatically whenever the exit() function executes
5
Understanding the Limitations of Traditional Error Handling
12 Understanding the Limitations of Traditional Error Handling A general rule of modular programming is that a function should be able to determine an error situation; but not necessarily take action on it The function in Figure 12-4 returns a 1 if the dataEntryRoutine() function detects an error, and a 0 if it does not
6
Understanding the Limitations of Traditional Error Handling
12 The calling main() function checks the return value of the function and takes appropriate action, printing an error message or not However, this error-handling technique has at least two drawbacks based on the following rules: A function can return, at most, only one value When a function returns a value, it must return only the type indicated as its return type In the example in Figure 12-4, you could work around the problem of losing one of the values by rewriting the dataEntryRoutine() function so that sometimes it returns the userEntry, and sometimes it returns the error code
7
Program that Uses dataEntryRoutine() with Error Code
12
8
Understanding the Limitations of Traditional Error Handling
12 Understanding the Limitations of Traditional Error Handling As another remedy, you could write the dataEntryRoutine() function so it accepts the address of the userEntry variable from main() However, allowing functions to access and alter passed variable values violates the general principle of encapsulation, and increases the data coupling of the function Second, any error code returned by a function must be the same type as the function’s return type
9
12 Throwing Exceptions A function can contain code to check for errors, and send a message when it detects an error In object-oriented terminology, an exception is a message (an object) that is passed from the place where a problem occurs to another place that will handle the problem The general principle underlying good object-oriented error handling is that any called function should check for errors, but should not be required to handle an error if one is found
10
Throwing Exceptions 12 When an object-oriented program detects an error within a function, the function should send an error message to the calling function, or throw an exception A throw resembles a return statement in a function, except that the execution of the program does not continue from where the function was called
11
Throwing Exceptions 12 You throw an exception by using the keyword throw followed by any C++ object, including an object that is a built-in scalar type, such as an int or a double; a nonscalar type, such as a string or numeric array; or even a programmer-defined object A function can make more than one throw
12
12 Throwing Exceptions When you use the version of the dataEntry() function shown in Figure 12-7, if the user enters a value between 0 and 12 inclusive, the actual value is returned to the calling function when the function ends
13
Using Try Blocks 12 A try block consists of one or more statements that the program attempts to execute, but that might result in thrown exceptions A try block includes the following components: The keyword try An opening curly brace The code that is tried A closing curly brace
14
A main() Function Containing a Try Block
12
15
Using Try Blocks 12 In Figure 12-8, the call to the dataEntry() function occurs within the highlighted try block When dataEntry() executes, if the userEntry is valid, then no exception is thrown, and main() executes to the end, using the valid value returned by the dataEntry() function If the dataEntry() function is called from a try block, as in Figure 12-8, then you can deal with the error situation more elegantly and less abruptly than with an exit() call You handle the thrown exception by catching it
16
12 Catching Exceptions To handle a thrown object, you include one or more catch blocks in your program immediately following a try block A catch block includes the following components: The keyword catch A single argument in parentheses An opening curly brace One or more statements that describe the exception action to be taken A closing curly brace Figure 12-9 shows a dataEntry() function that throws a string error message if the user enters a negative number
17
Catching Exceptions 12
18
Catching Exceptions 12 Ex12-2 After executing the catch block, the main() program continues The value shown is garbage because in the program, userValue is assigned a valid value only when dataEntry() is completed
19
Catching Exceptions 12 To avoid seeing this garbage value, you could take one of several actions: Initialize userValue when it is declared in the main() function; its value will change only if the try block is successful Assign a dummy value to userValue within the catch block, as well as within the try block. If the try block is successful, then userValue holds the value entered by the user. If the try block is unsuccessful, then userValue holds the assigned dummy value Declare a flag variable set to 0, then set it to 1 within the catch block to indicate an exception was thrown.
20
Catching Exceptions 12 If you want a catch block to execute, it must catch the correct type of argument thrown from a try block In the steps reviewed on pages 455 to 456 of the textbook, you create a passwordEntry() function that asks a user to supply a password
21
Catching Exceptions 12 Ex12-3
22
Using Multiple Throw Statements and Multiple Catch Blocks
12 Using Multiple Throw Statements and Multiple Catch Blocks You can write a function to throw any number of exceptions, and you can provide multiple catch blocks to react appropriately to each type of exception that is thrown You can create a dataEntry() function that throws a string message when the user enters a negative number, but throws an invalid value when the user enters a number that is more than 12 When you run the program in Figure 12-14, if no exception is thrown, the program bypasses both catch blocks and prints the valid value, as shown in Figure 12-15
23
A dataEntry() Function that Throws Two Types of Exceptions
12
24
Using Multiple Throw Statements and Multiple Catch Blocks
12 Ex12-4
25
12 Throwing Objects Just as simple variables such as doubles, integers, and strings can be thrown via exception-handling techniques, programmer-defined class objects also can be thrown This approach is particularly useful in two types of situations: If a class object contains errors, you may want to throw the entire object, rather than just one data member or a string message Whenever you want to throw two or more values, you can encapsulate them into a class object so that they can be thrown together
26
Throwing Standard Class Objects
12 Figure shows an Employee class with two data fields, empNum and hourlyRate The insertion operator is overloaded in the same way you have seen it coded in many other classes, but in this Employee class, the extraction operator has been overloaded to throw an exception Any program that uses the Employee class can catch the thrown Employee object and handle it appropriately for the application
27
The Employee Class 12
28
Throwing Standard Class Objects
12 A few possibilities include: A program might assign default values to any Employee whose data entry resulted in an exception A program that tests the accuracy of data entry operators might store caught exceptions just as they are entered and count them A program that is used when hiring Employees for a special assignment that pays more than the usual maximum might ignore the high-end salary violations A program might refuse to accept an Employee with an exception, and force the user to re-enter the values, as in the main() program shown in Figure 13-20
29
A main() Program that Instantiates Three Employee Objects
12 A main() Program that Instantiates Three Employee Objects
30
Throwing Standard Class Objects
12 Ex12-5 The program in Figure declares an array of three Employee objects When you include multiple catch blocks in a program, the first catch block that can accept a thrown object is the one that will execute
31
Throwing Exception Objects
12 You can create a class that represents an exception Figure shows a Customer class that is similar to many classes you already have worked with Each Customer holds data fields for a customer number and a balance due, and has access to overloaded extraction and insertion operators that you can use for input and output
32
The Customer Class 12
33
Throwing Exception Objects
12 Throwing Exception Objects Figure shows the CustomerException class Its data members include a Customer object and a string message, and its constructor requires values for both
34
Throwing Exception Objects
12 Figure shows a main() program that declares an array of four Customer objects In this program a loop contains four calls to the overloaded operator >>() function Figure shows the output of a typical execution of the program in which some of the Customers exceed the credit limit and others do not
35
Program that Instantiates Four Customers
12
36
Output of Typical Execution of the CustomerException Program
12 Output of Typical Execution of the CustomerException Program Ex12-6
37
Using the Default Exception Handler
12 Using the Default Exception Handler When any object is thrown with a throw statement, then a subsequent catch block has a usable match if one of the following conditions is true: The type of the thrown object and the type of the catch argument are identical (for example, int and int) The type of the thrown object and the type of the catch argument are the same, except the catch contains the const qualifier, a reference qualifier, or both (for example, int can be caught by const int, int&, or const int&) The catch argument type is a parent class of the thrown argument
38
Using the Default Exception Handler
12 If you throw an argument and no catch block exists with an acceptable argument type, then the program terminates To avoid termination, you can code a default exception handler that catches any type of object not previously caught In the steps outlined on pages 466 to 470 of the textbook, you demonstrate that a default catch block works as expected You create an Order class whose data entry function accepts several values and throws a variety of exception types
39
Output of OrderExceptions Program
12 Ex12-7
40
Unwinding the Stack 12 When you write a function, you can try a function call and, if the function you call throws an exception, you can catch the exception However, if your function that calls the exception-throwing function doesn’t catch the exception, then a function that calls your function can still catch the exception A simpler way to say this is that if function A calls function B, and function B calls function C, and function C throws an exception, then if B does not catch the exception, function A can
41
Unwinding the Stack 12 If no function catches the exception, then the program terminates This process is called unwinding the stack, because each time you call a function, the address of the place to which the program should return is stored in a memory location called the stack Figure shows a KennelReservation class that holds information about boarding a Do Each KennelReservation includes a kennel number, a month and day for the reservation, and a Dog
42
The Dog Class 12
43
The KennelReservation Class
12 Ex12-8
44
Unwinding the Stack 12 Within the KennelReservation function in Figure , both highlighted exception specifications could be deleted, and the class still would function properly Figure shows a main() function that instantiates a KennelReservation object, and Figure shows a typical execution
45
Unwinding the Stack 12
46
12 Summary A popular traditional way to handle error situations was to terminate the program A superior alternative to traditional error-handling techniques is called exception handling The general principle underlying good object-oriented error handling is that any function that is called should check for errors, but should not be required to handle an error if it finds one When a function might cause an exception, and therefore includes a throw statement to handle errors, the call to potentially offending functions should be placed within a try block
47
Summary 12 To handle a thrown object, you include one or more catch blocks in your program immediately following a try block If you want a catch block to execute, it must catch the correct type of argument thrown from a try block You can write a function to throw any number of exceptions, and you can provide multiple catch blocks to react appropriately to each type of exception that is thrown
48
Summary 12 Just as simple variables such as doubles, integers, and strings can be thrown via exception-handling techniques, so can programmer-defined class objects You can create a class that represents an exception The class is instantiated only when an exception occurs
49
Summary 12 If you throw an argument and no catch block exists with an acceptable argument type, then the program terminates You can explicitly indicate the exception that a function can possibly throw by writing an exception specification, which is a declaration of a function’s possible throw types When you allocate memory, it is always possible that there is not enough memory available, so the creators of C++ have created an out of memory exception handler for you called set_new_handler()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.