1 / 21

Exceptions

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

manning
Download Presentation

Exceptions

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 exception throw; } }

  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

  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.

More Related