Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exception Handling Oo28.

Similar presentations


Presentation on theme: "Exception Handling Oo28."— Presentation transcript:

1 Exception Handling Oo28

2 Teaching Points Exception handling mechanisms Try-Throw-Catch

3 Error Handling Styles When we detect an error (exceptional condition) we can: Terminate the program Return a value representing the error Return a legal value and place the program in an illegal state Call an error function BUT…

4 Terminate the program Too Drastic!
Many programs cannot just role over and fail Mission critical User can intervene to resolve error

5 Return a value representing the error
Clutter in the main line of the code. It becomes hard to see main line of the logic is. Often there is no acceptable value which can easily be used (eg. The possible returns already cover the entire set of return values)

6 Return a legal value and place the program in an illegal state
Clutter in the main line of the code. It becomes hard to see main line of the logic is. Probably must resort to global state variable (eg. C library errno) Example from C

7 Call an error function May clean up the logic a bit but we are really left with the same choices as before after the error call is made.

8 New Alternative Try-Throw-Catch
Analogy - an alternate return mechanism

9 Try-Throw-Catch try block
a program block in which an exception may be thrown it is uncluttered with error handling support any of the statements might throw an exception in which case the execution of the block stops with that statement

10 Try-Throw-Catch catch block
a separate block in the body of the operation not normally executed designed to handle a specific exception this block is executed immediately after the specified exception is detected in the try block

11 Try-Throw-Catch throw statement
stops execution of the operation at this statement throws an exception to the calling operation (I.e. does an alternate return with the exception value instead of the normal return value) the thown value is an object exceptions have their own types and are not related to the normal return type for the operation

12 Example Example is in Java but is essentially the same mechanism as C++

13 Teaching Points Exception handling mechanisms Try-Throw-Catch


Download ppt "Exception Handling Oo28."

Similar presentations


Ads by Google