CIS 29 - Notes for Thursday, 2/4

Announcements and Reminders

  • Assignment 4 is due Tuesday
  • Group Meeting #2 is due Saturday
  • Midterm is next Thursday
Recording

Exception Handling

  • What is exception handling?
Error handling
Organized and consistent
Fault tolerant processing
  • When is it appropriate?
  • It will change your programming style
  • The approach mostly consists of a try block, a throw, and a catch block.

Previous Methods

Example 10-1 - The assert macro

Example 10-2 - longjump

Exception Handling Basics

Example 10-3 - try, throw, catch

Example 10-4 - Handling a file open error

Example 10-5 - Where to throw, where to catch

Example 10-6 - Throwing and catching more than one type

Example 10-7 - Unhandled exceptions

Example 10-8 - How to catch anything

Example 10-9 - Exception handling classes

Example 10-10 - Use a class to access different values that may be thrown

Example 10-11 - set_terminate()

Exception Specifications

Dynamic exception specifications are no longer supported since C++17. 

 
Examples 

void funk1() throw (sometype); // Error: not allowed in C++17
void funk2() throw ();         // Error: not allowed in C++17
void funk2() noexcept;         // OK

set_unexpected()

The set_unepected() function was removed in C++17.

Example 10-14 - Re-throwing a throw

Example 10-15 - Unwinding the stack

Example 10-16 - Standard Exceptions  (comment added about bad_cast exception added 2/4)

Example 10-17 - Derive your own exceptions from standard exceptions 

Exception Handling - loose ends

  • Throw by value, catch by reference
  • The order of your catches may be important
try
{
   ...
   throw runtime_error(whatever);
   ...
   throw InvalidThingCode(whatever);
   ...
   throw FileOpenException(whatever);
   ...
}
catch (runtime_error arg)
{
...
}
catch (InvalidThingCode &arg)
{
...
}
catch (const FileOpenException& arg)
{
...
}
...