Download presentation
Presentation is loading. Please wait.
Published byEdmund Mathews Modified over 6 years ago
0
Introduction to C++ computers and programming
CMPT 129 © Janice Regan, CMPT
1
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program such as Failure to allocate memory Dividing by 0 The programmer can also generate exceptions to deal with possible serious conditions/errors in his program © Janice Regan, CMPT
2
Throwing Exceptions When exceptions happen we say that an exception is thrown When an exception is thrown by our program we should have error management code in our program to catch the exception and process the error condition Uncaught exceptions can lead to unexpected program terminations that can be difficult to localize © Janice Regan, CMPT
3
When to use exceptions We already know how to use decision statements (ifs) to test if a dangerous situation or error has occurred. We know how to manage the errors so the user of the program receives useful / needed information However, when we use decision statements to manage errors in functions we often lose important information on when/where the error occurred ( which function called the function that failed … ). We can use exceptions to improve this © Janice Regan, CMPT
4
Returning Error information from functions (1)
When an error condition is detected by a decision statement in a function We can pass an error condition back to the calling function Usually this means a “special” value of the of the variable being returned -1 <= sin(x) <= x, so -99 could be chosen as a “special” value to indicate an error in sin(x) -∞ <= 2x <= ∞, so there is no “special” value to indicate an error in 2x. How can we pass back an error value in this case. Answer: use exceptions © Janice Regan, CMPT
5
Using EXCEPTIONs To use exceptions we use two new C++ keyword try {
// code to check catch ( exceptionParameter ) // code to manage errors } © Janice Regan, CMPT
6
Explaining using exceptions
The try block is the code between the { } following the keyword try The catch block is the code between the { } following the keyword catch The try block will execute one statement at a time. When one of the statements in the try block throws an exception the next statement executed will be at the start of the catch block, When the catch block finishes, then the code following the end of the catch block will execute © Janice Regan, CMPT
7
Exceptions and functions
To throw an exception you do not need a try or catch block. To catch an exception you need both a try and a catch block Assume the main function calls function B which calls function C In function C an exception is throw. C may not need a try or catch block. The exception may be caught in main function or function B If the exception is caught in function B it can be rethrown so it can also be caught in main The serious errors that cause termination of the program should be caught an processed in main © Janice Regan, CMPT
8
Throwing an exception An exception is usually thrown in place of printing an error message and taking an action like terminating the program inside the function When a function throws an exception it then terminates and passes execution to its calling function Assume the following exception is thrown in function myPrint if ( myStream.fail() ) { throw “ERROR: cannot read input \n “; } © Janice Regan, CMPT
9
Catching an exception Assume the main function called function myCalc which called function myPrint. An exception was thrown in myPrint (previous slide) In myCalc or main the exception can be caught and processed as follows try { myPrint (x, y, z); } catch (char* exceptionString) { // prints ERROR: cannot read input cout << exceptionString; } © Janice Regan, CMPT
10
Why rethrow EXCEPTIONS
In our example main calls function myCalc calls function myPrint Some types of exceptions generated in myPrint may be processed in myPrint, others in myCalc, and others in main A catch catches all exceptions that of a particular type (string, code, C++ exception) Exceptions to be processed in main that are caught in myCalc (when exceptions to be processed in myCalc are caught) must be rethrown by myCalc © Janice Regan, CMPT
11
Catching / rethrowing an exception
A function can take an action like printing a message before passing the exception to the function that called it myCalc can indicate that it called myPrint before sending the exception to the main program catch (const char* exceptionString) { cerr << “in this function MyCalc" << “ MyPrint was called” << endl; throw exceptionString; } © Janice Regan, CMPT
12
You Can also send eRROR codes
So far we have sent the string containing the error message in the exception handler An integer error code can also be sent if ( !(mystream >> myVar) ) { throw 10; } © Janice Regan, CMPT
13
Catching Error codes When an integer code is thrown it can be caught as shown below catch(int code) { cout << “number is not an integer”; } © Janice Regan, CMPT
14
Catching C++ generated exceptions
When C++ throws an exception it can be caught as shown below. C++ throws exceptions for serious errors that would require the program be terminated Examples include, failing to allocate memory and dividing by 0 catch(exception& e) { cout << e.what(); //prints message } © Janice Regan, CMPT
15
MULTIPLE EXCEPTIONS Inside one try block it is possible that multiple exceptions can be thrown. Some of the exceptions may send strings to be caught Some of the exceptions may send codes to be caught Each of the possible types of exception need to be processed in a separate catch block Exceptions that throw different codes will be processed in one catch block Exceptions that throw different strings will be processed in another block Exceptions thrown by the system will be processed in other blocks © Janice Regan, CMPT
16
Using multiple catch blocks
try { // code generating many different exceptions } catch(int code) // code processing exceptions that generate error codes catch(char *msg) // code processing exceptions than generate messages } catch( exception& e) // code processing C++ generated exceptions © Janice Regan, CMPT
17
MULTIPLE EXCEPTIONS Inside one catch block it is possible that multiple different exceptions of the same type can be caught. Each of the possible different exceptions need to be processed in the catch block Assume function A calls function B calls function C Some types of exceptions generated in C may be processed in C, others in B, and others in A A catch catches all exceptions that of a particular type Exceptions to be processed in A that are caught in B must be rethrown by B © Janice Regan, CMPT
18
Catching Multiple Error codes
The catch block for integer codes can catch and process any number of different codes catch(int code) { if(code == 10) cout << “number is not an integer”; } //process any other codes to be managed in // this functions as else ifs, // any code may be re thrown after processing else throw code; //these codes need to be processed in calling function //no action needs to be tacken in this function © Janice Regan, CMPT
19
Catching Multiple error messages
The catch block for string messages can catch and process any number of different codes catch(char* msg) { if(msg == “number in file not in range”;) cout << “range is 0< x < 23”; cout << endl << msg; throw msg; } //process any other codes in else ifs // codes will only be re thrown if the exception // needs to be passed to the calling program © Janice Regan, CMPT
20
Correcting exception conditions
In some cases we were able to correct errors For example clearing the stream and rereading after attempting to read an integer and not being able to read any numbers We can also use try catch to recover in these cases For example our input loop for rereading data can be replaced by a try and catch inside a loop that repeats up to the maximum number of retries (see posted example) © Janice Regan, CMPT
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.