Download presentation
Presentation is loading. Please wait.
1
Throwing and catching exceptions
Error Handling in C++ Throwing and catching exceptions T Update for VS The only area where Java is superior Copyright © Curt Hill
2
Copyright © 1999-2014 - Curt Hill
Exceptions Exception: An unexpected problem that prevents normal continuation of the algorithm Examples: divide by zero using a NULL pointer It could be a program specific error as well Copyright © Curt Hill
3
Traditional Exception Handling
In many languages there is no error handling If we think there is a good chance that a divisor could be zero, we could test it: if(k==0) { // handle error … else j = m/k; Copyright © Curt Hill
4
Copyright © 1999-2014 - Curt Hill
Problems There are just too many possible ways to generate an exception If we tested each one with an if most of the code would be error testing We set a trap instead that covers more of the program Copyright © Curt Hill
5
Copyright © 1999-2014 - Curt Hill
Another Possibility There exists an include: <assert.h> This contains a function: assert(int) This is used for testing assertions If the int evaluates to a true the assert does nothing If false it aborts the program with an error message Copyright © Curt Hill
6
Copyright © 1999-2014 - Curt Hill
Example Consider the following code: int array[MAX]; … index = …; assert(index>=0 && index<MAX); array[index] = …; This will abort the program if the index is not in the correct range Copyright © Curt Hill
7
Copyright © 1999-2014 - Curt Hill
Assert Issues Asserts are typically used to test assumptions Their being false generally indicates a logic error earlier in the program Bad computations Lack of testing of input Favorite way to abort a program: assert(false); There is also no recovery Makes it undesirable for error checking of an expected nature Copyright © Curt Hill
8
C++ Exception Handling
There is a better way to handle errors that are not logic errors C++ exception handling is having one error handling routine that intercepts all divide by zero exceptions At least all that are in the same call chain The exception handler can be a long ways from the exception Copyright © Curt Hill
9
Copyright © 1999-2014 - Curt Hill
Two Parts Finding the error and announcing it as an error Throwing an exception This uses the throw keyword Handling the exception Recovery or error handling This uses the try and catch keywords Copyright © Curt Hill
10
Catching the exception
Uses the try catch block try introduces a compound statement This is followed by one or more catch clauses Each catch clause is like a one parameter method that handles that kind of exception Copyright © Curt Hill
11
Copyright © 1999-2014 - Curt Hill
try catch syntax Form is: try { // code } catch (ExceptionType_1 e) { handle exception 1} catch (ExceptionType_2 e) { handle exception 2 } ... catch (ExceptionType_N e) { handle exception N } Copyright © Curt Hill
12
Copyright © 1999-2014 - Curt Hill
Form of catch catch (type parm){stmts} Parameter is any type The catch matches the type of the exception Limited scope, only usable in the compound statement Compound statement is like a method body and the Exception (or subclass) is the only parameter Copyright © Curt Hill
13
Copyright © 1999-2014 - Curt Hill
Catch Parameter Needs to match the throw parameter Carries information from the site of the error to the catch clause Any type may be thrown and later caught System errors are usually char * The error type of an elipsis will catch anything: catch(…) { } but gives no information Copyright © Curt Hill
14
Copyright © 1999-2014 - Curt Hill
Example try catch Catch conversion error: try { d = str.ToDouble(); } catch(wxString e){ MessageBoxW (NULL,e, L”oops”,MB_OK); } The error can be in the try or in any function called from within the try Copyright © Curt Hill
15
Exceptions and the throw
Many exceptions are thrown automatically These require no intervention on our part They typically throw a C style string, which is the message Copyright © Curt Hill
16
Copyright © 1999-2014 - Curt Hill
Throwing an Exception Use reserved word throw and the new type Example: if(j==0) throw “j was zero\n”; User defined throws may place a suffix on the function: int func(…) throw (char *) { indicates the exceptions that could be thrown Copyright © Curt Hill
17
Copyright © 1999-2014 - Curt Hill
Cause and Effect The throw and catch do not have to be near each other The throw is like a super return The current function is exited as well as any between this one and the function containing the try and catch Consider in next screen, a main function with a try catch that calls Fun_1, which calls Fun_2, which calls Oops, which throws an exception Copyright © Curt Hill
18
Copyright © 1999-2014 - Curt Hill
Call example throw Oops Oops(...); Fun_2 Fun_2(…) Fun_1 try{ Fun_1(…) catch(…) main Copyright © Curt Hill
19
Copyright © 1999-2014 - Curt Hill
Exception Processing The call stack between throw and catch is flushed Every call, every parameter and every local is destroyed The function and all those in between are terminated They may be re-executed later, but they can not be resumed Copyright © Curt Hill
20
Copyright © 1999-2014 - Curt Hill
Nested Trys We do not usually nest a try within another However, to get to our exception we may go through several try catch pairs in several methods The exception is always caught by the closest try Closest in terms of most recently called function Copyright © Curt Hill
21
Copyright © 1999-2014 - Curt Hill
Second call example throw Oops Oops(...); Fun_2 try { Fun_2(…); catch(…){…} Fun_1 try{ Fun_1(…) catch(…){…} main Copyright © Curt Hill
22
Copyright © 1999-2014 - Curt Hill
Multiple Catches A try catch statement can have many catch clauses They are tried in order from top to bottom What the clause handles is based on the type of the parameter to the catch The first one that matches handles the error Copyright © Curt Hill
23
The Catch Parameter Type
Two catch parameters of different types usually cannot interfere with each other Thus the order of: catch (int ie) {…} catch (char * ce) {…} does not matter This is not true if the parameters are in the same inheritance hierarchy Copyright © Curt Hill
24
Copyright © 1999-2014 - Curt Hill
Recall Inheritance maintains an “is a” relationship If a person is a class and student is derived from that person class, then student is a person Thus inheritance can influence the order of catch clauses following a try block Copyright © Curt Hill
25
Inheritance and Catch Parameters
Suppose: class Exc {…}; class Exc2:public Exc {…}; Exc2 has an isa relationship with Exc Thus the order of: catch (Exc e) {…} catch (Exc2 ce) {…} does matter The first one catches a thrown Exc and the second catches Exc2 Copyright © Curt Hill
26
Copyright © 1999-2014 - Curt Hill
Again Suppose: class Exc {…}; class Exc2:public Exc {…}; Thus the order of: catch (Exc ce) {…} catch (Exc2 e) {…} does matter Since Exc2 isa Exc the first catch will catch both Copyright © Curt Hill
27
Copyright © 1999-2014 - Curt Hill
Right and Wrong Exc2 is a descendant of Exc Thus the order of: catch (Exc2 ce) {…} catch (Exc e) {…} both catches may catch something Thus the order of: catch (Exc ce) {…} catch (Exc2 e) {…} only the first may catch something Copyright © Curt Hill
28
Multiple trys and catches
There can be as many try catch statements as wanted The smaller the area protected, the more specific the exception handling can be The larger the area the more coverage the exception handling Copyright © Curt Hill
29
Copyright © 1999-2014 - Curt Hill
Throw and Throw Again A catch may end up with an exception that it does not know how to handle It merely issues a throw and lets someone else handle it It better be nested in an if so that all exceptions are not thrown Copyright © Curt Hill
30
User Defined Exceptions
A truly systematic programmer will define their own exception classes These will be thrown instead of simple things like int or char * Some libraries, such as VCL, already have an exception class that may be sub-classed These are often used for application specific problems Copyright © Curt Hill
31
Classes and Exceptions
A try catch only protects a single call chain There is no standard means for protecting every method of a class This includes the Form class as well Each public method needs it’s own try catch for full protection Copyright © Curt Hill
32
Copyright © 1999-2014 - Curt Hill
Differing Systems Unlike other languages, such as Java, the libraries are not standard in C++ Each implementation can have their own system of error objects Embarcadero’s CBuilder and Microsoft’s Visual Studio both have an elaborate class system Dev-C++ does not Copyright © Curt Hill
33
Copyright © 1999-2014 - Curt Hill
New Things Keywords try catch throw Class Exception Copyright © Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.