Exceptions - PowerPoint PPT Presentation

1 / 21

  • Uploaded on
  • Presentation posted in: General

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Presentation


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript





  • 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

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.

Handling errors

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

Handling exceptions

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



  • 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

Try blocks

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 insidetry blocks

Catch clauses

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

Try catch block

Try/catch block

  • Catch blocks can only be entered by catching thrown statements

  • Syntax


    //program statements


    catch (exception-specifier){

    //handler statements

    }//…can have more than one catch clause



  • 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

Functions can tell compiler what type of throw

Functions can tell compiler what type of throw

  • throw()

    • Tells the compiler the function does not throw an exception

    • void myFunction(inti) throw();

  • throw(…)

    • Tells the compiler the function can throw an exception

    • void myFunction(inti) throw(…);

  • throw(type)

    • Tells the complier the function can only throw an exception of type type

    • void myFunction(inti) throw(int);



  • 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




  • 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() {


    //program statements

    }catch(…) {

    //work to partially handle the exception




Standard exceptions class

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

Standard exceptions

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.

Standard exceptions hierarchy

Standard exceptions Hierarchy

Stack unwinding

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

Catchable types

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



  • Exceptions cannot remain unhandled

  • If no matching catch is found, terminate is called

  • terminate is a library function which ends your program




  • 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.

  • Login