Exceptions
description
Transcript of Exceptions
1
EXCEPTIONS
2
ERRORS Low-level errors
Divide by 0 Dereference a null pointer
High-level, logical errors Inserting past the end of a list Calling list.get(int) when the list is empty
Require immediate handling
3
CAUSE OF ERRORS User error
Giving a bad file name Trying to undo when nothing has been done
Programmer error Bugs
These errors are dealt with by an exception class.
4
HANDLING ERRORS
Exceptions deal with errors
The exception class passes information about the error that just occurred from where the error is detected to where the error will be resolved
Exceptions allow us to separate problem detection from problem resolution
5
HANDLING EXCEPTIONS Throw the exception
Catch and handle the exception
Catch it, then re-throw it or throw a different exception
Ignore the exception If there is no handler to deal with the exception,
then a default terminate function is called
6
THROW Exceptions are “thrown”
To a part of the code that can handle them Halts current code Control transferred to a “catch clause”
When a program runs into an error that it cannot handle
Many throw expressions take a string initializer but not all
7
TRY BLOCKS Try blocks wrap a series of statements
Can be nested Followed by one or more catch clauses
Try block is a local scope Any variable declared inside is not accessible
outside of the block (including catch clauses)
Exceptions are “thrown” from inside try blocks
8
CATCH CLAUSES Catch what is thrown Catch clause has 3 parts:
Keyword catch Exception specifier (the type of exception) And a block of code
Often called “handlers” because they handle the exception
The order handlers appear is important For a specific try block, the catch clauses are
examined in order of their appearance
9
TRY/CATCH BLOCK
Catch blocks can only be entered by catching thrown statements
Syntax try{
//program statements} catch (exception-specifier){
//handler statements}//…can have more than one catch clause
10
RETHROW
If a catch cannot completely handle an exception, the catch clause can throw the exception to another catch further up the list of function calls
Not followed by a type or expression Syntax:
throw; can only appear in a catch
11
FUNCTIONS CAN TELL COMPILER WHAT TYPE OF THROW throw()
Tells the compiler the function does not throw an exception
void myFunction(int i) throw(); throw(…)
Tells the compiler the function can throw an exception
void myFunction(int i) throw(…); throw(type)
Tells the complier the function can only throw an exception of type type
void myFunction(int i) throw(int);
12
THROW
We can throw exceptions in the middle of a function
//…if(!<some true condition>)
throw runtime_error(“Not true.”);//if the statement was true, just continue on//…
13
CATCH Similar to throw, catch(…) is a catch-all handler
It can catch an exception of any type It is often used with a rethrow expression Syntax:
void myFunction() {try{
//program statements}catch(…) {
//work to partially handle the exceptionthrow;
}}
14
STANDARD EXCEPTIONS CLASS exception
Most general kind of problem Provides little information about the error, only
that it has occurred Takes in NO string initializer
runtime_error Can only be detected at run time Some more specific types of runtime_error
range_error : outside of meaningful value range overflow_error : computation that overflowed
15
STANDARD EXCEPTIONS logic_error
Detected before run time Some more specific logic_errors
invalid_argument : inappropriate argument length_error : attempt to create an object larger than
the maximum size for that type out_of_range : used a value outside of the valid range
Both runtime and logic errors require string initializers Used to provide additional information about the
error that occurred.
16
STANDARD EXCEPTIONS HIERARCHY
exception
runtime_error
overflow_error range_error
logic_error
length_error invalid_argument
17
STACK UNWINDING When looking for a catch, each function in
which a catch is not found is popped off the stack If a throw is in a try block, then the catch clause
associated with the try block is examined first If no matching catch, then this function is exited
and the search continues in the function that called this one
When a function exits due to an exception, local objects are destroyed properly
18
CATCHABLE TYPES
When an exception is thrown, it can be caught by the following handlers: One that can catch any type One that accepts the same type as was thrown
Or a reference to the same type as was thrown One that accepts the base class as the type
thrown Or a reference to the same base class as was thrown A catch handler for a base class must not precede the
catch handler for the derived class
19
TERMINATE
Exceptions cannot remain unhandled
If no matching catch is found, terminate is called
terminate is a library function which ends your program
20
QUESTIONS?
21
In tic-tac-toe, a player can only enter X’s and O’s. Write an exception to deal with a player entering something other than X’s and O’s.