Exception handling --------------- Today we will discuss basic exception handling relative to the text. There is also a the handouts from Prof. Izaguire that we will discuss Friday. An exception is a problem that happens during runtime, As the video in class suggests, we want to correct these issues rather than give up (or worse). This will let you handle, or resolve, problems without your program crashing. This enables fault tolerant programs. Even more important, the capacity in C++ lets you do this in a standard manner, allowing teams of developers to work together. The basic idea of exception handling is: Do something If the something went wrong, do some error processing here or elsewhere As compared to C programs where you would do something like: Do something Check if it went wrong... Do something Check if it went wrong... This isn't preferred as program bits and error-handling bits are all jumbled together, making the program harder to read and debug. Instead, we introduce try and catch blocks. Problematic code is placed inside a subsection of code preceded by try. If something goes wrong, we "throw" an exception using standard C++ error handling objects (see Fig 16.1 for an example). The "throw" is handled by a corresponding "catch", allowing your program to correct the mistake (if possible) and move on. Some notes: 1.) you can not place code in-between try and catch blocks. 2.) you can only have a single parameter in a catch block 3.) catch blocks typically receive objects by reference to limit overhead of copying 4.) you can not have two different catch handlers after a try block, so you'll need to use this feature accordingly 5.) Try to incorporate exception handling when you are designing code, as doing it later can be hard. 6.) you can rethrow an exception if you want AND 7.) if there is no corresponding catch in the current block, it will try and find the next catch statement available. If this happens, large pieces of code may not be run (see in-class example).' Why exception handling is important: 1.) A single, uniform mechanism for processing errors 2.) Simplifies combining different classes together 3.) More efficent than error-correcting logic as little overhead is needed 4.) Exceptions are for the rare, really bad stuff. Keeps your programs working if needed.