Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exception Handling C++.

Similar presentations


Presentation on theme: "Exception Handling C++."— Presentation transcript:

1 Exception Handling C++

2 Exception Handling in C++
Added to C++ in 1990 Exceptions are user- or library-defined (none in language definition) Exception Handlers Form (like Java): try { -- code that is expected to raise an exception } catch (formal parameter) { // only 1 parm -- handler code ... catch (formal parameter) { Exception Handling in C++

3 C++: The catch Function
catch is the name of all handlers--it is an overloaded name, so the formal parameter of each must be unique The formal parameter need not have a variable It can be simply a type name to distinguish the handler it is in from others, e.g. catch(int) The formal parameter variable can be used to transfer information to the handler The formal parameter can be an ellipsis, in which case it handles all exceptions not yet handled – how is this done in Java? C++: The catch Function

4 C++: Throwing Exceptions
Exceptions are all raised explicitly by the statement: throw expression; expression may be any type – compare to Java? A throw without an operand can only appear in a handler; when it appears, it simply re-raises the exception, which is then handled elsewhere C++: Throwing Exceptions

5 Simple throw/catch example
int num; char ch; double dNum; try { cout << "Enter int from : "; cin >> num; if (num < 1 || num > 100) throw num; cout << "Enter 'A' or 'B' : "; cin >> ch; if (ch != 'A' && ch != 'B') throw ch; cout << "Enter double : "; cin >> dNum; if (dNum < 1.1 || dNum > 1.5) throw dNum; } catch(int numIn) { cout << numIn << " is not in the range\n"; } catch (char charIn) { cout << charIn << " is not A or B\n"; catch (...){ cout << "Invalid input!\n"; "Continuing on...\n"; Simple throw/catch example Copyright © 2006 Addison-Wesley. All rights reserved.

6 C++: Unhandled Exceptions
An unhandled exception is propagated to the caller of the function in which it is raised This propagation continues to the main function If no matching handler is found through propagation, the default handler (unexpected) is called The default handler simply terminates the program C++: Unhandled Exceptions Copyright © 2006 Addison-Wesley. All rights reserved.

7 After a handler completes its execution, control flows to the first statement after the last handler in the sequence of handlers of which it is an element C++: Continuation?

8 Simple Propagation Example
int doInput(); int main() { int num; try { doInput(); }catch(int numIn) { cout << numIn << " is not in the range\n"; system("pause"); exit(1); } cout << "Continuing on...\n"; int doInput() { int num; cout << "Enter a number from : "; cin >> num; if (num < 1 || num > 100) throw num; return num; } Simple Propagation Example Copyright © 2006 Addison-Wesley. All rights reserved.

9 C++: Other Design Choices
All exceptions are user-defined Functions can list the exceptions they may raise (but not required – unlike checked) The default handler can be replaced by a user-defined handler. Must be a void function that takes no parameters. C++: Other Design Choices


Download ppt "Exception Handling C++."

Similar presentations


Ads by Google