exceptions

21
EXCEPTIONS 1

Upload: manning

Post on 23-Feb-2016

31 views

Category:

Documents


0 download

DESCRIPTION

Exceptions. 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. Cause of errors. User error Giving a bad file name - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Exceptions

1

EXCEPTIONS

Page 2: 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

Page 3: Exceptions

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.

Page 4: Exceptions

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

Page 5: Exceptions

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

Page 6: Exceptions

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

Page 7: Exceptions

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

Page 8: Exceptions

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

Page 9: Exceptions

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

Page 10: Exceptions

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

Page 11: Exceptions

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);

Page 12: Exceptions

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//…

Page 13: Exceptions

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;

}}

Page 14: Exceptions

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

Page 15: Exceptions

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.

Page 16: Exceptions

16

STANDARD EXCEPTIONS HIERARCHY

exception

runtime_error

overflow_error range_error

logic_error

length_error invalid_argument

Page 17: Exceptions

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

Page 18: Exceptions

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

Page 19: Exceptions

19

TERMINATE

Exceptions cannot remain unhandled

If no matching catch is found, terminate is called

terminate is a library function which ends your program

Page 20: Exceptions

20

QUESTIONS?

Page 21: Exceptions

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.