Title: Concordia University Department of Computer Science and Software Engineering COMP345
1Concordia UniversityDepartment of Computer
Science and Software EngineeringCOMP345
Advanced Program Design with CLecture 9
Exception Handling
2Learning Objectives
- Exception Handling Basics
- Defining exception classes
- Multiple throws and catches
- Exception specifications
- Programming Techniques for Exception Handling
- When to throw exceptions
- Exception class hierarchies
3Introduction
- Typical approach to development
- Write programs assuming things go as planned
- Get "core" working
- Then take care of "exceptional" cases
- C exception-handling facilities
- Handle "exceptional" situations
- Mechanism "signals" unusual happening
- Another place in code "deals" with exception
4Exception-Handling Basics
- Meant to be used "sparingly"
- In "involved" situations
- Difficult to teach such large examples
- Approach
- Simple "toy" examples, that would notnormally
use exception-handling - Keep in mind "big picture"
5Introduction to Exception Handling
- Sometimes the best outcome can be when nothing
unusual happens - However, the case where exceptional things happen
must also be prepared for - C exception handling facilities are used when
the invocation of a method may cause something
exceptional to occur
6Introduction to Exception Handling
- C library software (or programmer-defined code)
provides a mechanism that signals when something
unusual happens - This is called throwing an exception
- In another place in the program, the programmer
must provide code that deals with the exceptional
case - This is called handling the exception
7try-throw-catch Mechanism
- The basic way of handling exceptions in C
consists of the try-throw-catch trio - The try block contains the code for the basic
algorithm - It tells what to do when everything goes
smoothly - It is called a try block because it "tries" to
execute the case where all goes as planned - It can also contain code that throws an exception
if something unusual happens - try
-
- CodeThatMayThrowAnException
8try-throw-catch Mechanism
- throw new ExceptionClassName(PossiblySomeArguments
) - Or
- throw object
- When an exception is thrown, the execution of the
surrounding try block is stopped - Normally, the flow of control is transferred to
another portion of code known as the catch block - The value thrown is the argument to the throw
operator, and is always an object (possibly an
instance of some exception class) - The execution of a throw statement is called
throwing an exception
9try-throw-catch Mechanism
- A throw statement is similar to a method call
- throw new ExceptionClassName(SomeString)
- In the above example, the object of class
ExceptionClassName is created using a string as
its argument - This object, which is an argument to the throw
operator, is the exception object thrown - Instead of calling a method, a throw statement
calls a catch block
10try-throw-catch Mechanism
- When an exception is thrown, the catch block
begins execution - The catch block has one parameter
- The exception object thrown is plugged in for the
catch block parameter - The execution of the catch block is called
catching the exception, or handling the exception - Whenever an exception is thrown, it should
ultimately be handled (or caught) by some catch
block
11try-throw-catch Mechanism
- catch(Exception e)
-
- ExceptionHandlingCode
-
- A catch block looks like a method definition that
has a parameter - It is not really a method definition, however
- A catch block is a separate piece of code that is
executed when a program encounters and executes a
throw statement in the preceding try block - A catch block is often referred to as an
exception handler - It can have at most one parameter
12try-throw-catch Mechanism
- catch(Exception e) . . .
- The identifier e in the above catch block heading
is called the catch block parameter - The catch block parameter does two things
- It specifies the type of thrown exception object
that the catch block can catch (e.g., an
Exception class object above) - It provides a name (for the thrown object that is
caught) on which it can operate in the catch
block - Note The identifier e is often used by
convention, but any non-keyword identifier can be
used
13try-throw-catch Mechanism
- When a try block is executed, three things can
happen - 1. No exception is thrown in the try block
- The code in the try block is executed to the end
of the block - The catch block is skipped
- The execution continues with the code placed
after the catch block
14try-throw-catch Mechanism
- 2. An exception is thrown in the try block and
caught in the catch block - The rest of the code in the try block is skipped
- Control is transferred to a following catch block
- The thrown object is plugged in for the catch
block parameter - The code in the catch block is executed
- The code that follows that catch block is
executed (if any)
15try-throw-catch Mechanism
- 3. An exception is thrown in the try block and
there is no corresponding catch block to handle
the exception - The rest of the code in the try block is skipped
- The function throws the exception to its calling
function. - The calling function either catches the exception
using a catch block, or itself throws the
exception. - If all functions fail to catch the exception, the
exception will eventually be thrown all the way
to the main function. If the main function cannot
catch the exception, the program ends, itself
throwing the exception.
16Toy Example
- Imagine people rarely run out of milk
- cout ltlt "Enter number of donuts"cin gtgt
donutscout ltlt "Enter number of glasses of
milk"cin gtgt milkdpg donuts/static_castltdoubl
egt(milk)cout ltlt donuts ltlt "donuts.\n" ltlt
milk ltlt "glasses of milk.\n" ltlt "You have " ltlt
dpg ltlt "donuts for each glass of milk.\n" - Basic code assumes never run out of milk
17Toy Example if-else
- Notice If no milk?divide by zero error!
- Program should accommodate unlikelysituation of
running out of milk - Can use simple if-else structure
- if (milk lt 0) cout ltlt "Go buy some
milk!\n"else - Notice no exception-handling here
18Toy Example with Exception Handling Display 18.2
Same Thing Using Exception Handling
19Toy Example Discussion
- Code between keywords try and catch
- Same code from ordinary version, exceptif
statement simplerif (milk lt 0) throw
donuts - Much cleaner code
- If "no milk" ? do something exceptional
- The "something exceptional" is providedafter
keyword catch
20Toy Example try-catch
- Try block
- Handles "normal" situation
- Throws an exception on an exceptional situation
- Catch block
- Handles "exceptional" situations
- Provides separation of normal from exceptional
- Not big deal for this simple example,
butimportant concept
21try block
- Basic method of exception-handling
istry-throw-catch - Try blocktry Some_Statements ltsome code
with a throws statement or a function invocation
that might throw an exceptiongt Some_More_Stateme
nts - Contains code for basic algorithm when allgoes
smoothly - A try block must be followed by at least one
catch block
22throw
- Inside try-block, when something unusual
happenstry Code_To_Try if
(exceptional_happened) throw object More_Code
- Keyword throw followed by an object id
- Called "throwing an exception
- An exception can be thrown outside of a try
block. - Upon throwing the exception, the function will
halt and throw the exception to the calling
function without even trying to catch it.
23catch-block
- When something thrown ? goes somewhere
- In C, flow of control goes from try-block
tocatch-block - try-block is "exited" and control passes to
catch-block - Executing catch block called "catching
theexception" - Exceptions must be "handled" in somecatch block
- There cannot be a catch block without a preceding
try block.
24catch-block More
- Recallcatch(int e) cout ltlt e ltlt " donuts,
and no milk!\n" ltlt " Go buy some milk.\n" - Looks like function definition with int
parameter! - Not a function, but works similarly
- Throw like "function call"
25catch-block Parameter
- Recall catch(int e)
- "e" called catch-block parameter
- Each catch block can have at most ONEcatch-block
parameter - Does two things
- type name specifies what kind of thrownvalue the
catch-block can catch - Provides name for thrown value caughtcan "do
things" with value
26Defining Exception Classes
- throw statement can throw value of any type
- Exception class
- Contains objects with information to be thrown
- Can have different types identifying each
possible exceptional situation - Still just a class
- An "exception class" due to how its used
27Exception Class for Toy Example
- Considerclass NoMilkpublic NoMilk()
NoMilk(int howMany) count(howMany) int
getcount() const return count private int
count - throw NoMilk(donuts)
- Invokes constructor of NoMilk class
28Inheriting from exception Class
- include ltiostreamgt include ltexceptiongt using
namespace std class myexception public
exception virtual const char what() const
throw() return "My exception happened"
// what() is a virtual method of exception
myex int main () try throw myex
catch (exception e) cout ltlt e.what() ltlt
endl return 0
29Using the exception Class
- All exceptions thrown by components of the C
Standard library throw exceptions derived from
the stdexception class - bad_alloc thrown by new on allocation failure
- bad_cast thrown by dynamic_cast when fails with
a referenced type - bad_exception thrown when an exception type
doesn't match any catchtry int myarray
new int1000 catch (bad_alloc) cout
ltlt "Error allocating memory." ltlt endl
30Multiple Throws and Catches
- try-block typically throws any number
ofexception values, of differing types - Of course only one exception thrown
- Since throw statement ends try-block
- But different types can be thrown
- Each catch block only catches "one type"
- Typical to place many catch-blocks after
eachtry-block - To catch "all-possible" exceptions to be thrown
31Catching
- Order of catch blocks important
- Catch-blocks tried "in order" after try-block
- First match handles it!
- Considercatch ()
- Called "catch-all", "default" exception handler
- Catches any exception
- Ensure catch-all placed AFTER more
specificexceptions! - Or others will never be caught!
32Trivial Exception Classes
- Considerclass DivideByZero
- No member variables
- No member functions (except defaultconstructor)
- Nothing but its name, which is enough
- Might be "nothing to do" with exception value
- Used simply to "get to" catch block
- Can omit catch block parameter
33Throwing Exception in Function
- If a function does not catch an exception
encountered during its execution, the function
itself will throw the exception - It is then expected that its calling function
will catch the exception - Callers might have different "reactions"
- Some might desire to "end program"
- Some might continue, or do something else
- Makes sense to "catch" exception incalling
functions try-catch-block - Place call inside try-block
- Handle in catch-block after try-block
34Throwing Exception in Function Example
- Considertry quotient safeDivide(num,
den)catch (DivideByZero) - safeDivide() function throws DividebyZero
exception - Handled back in callers catch-block
35Function Exception Specification
- Functions that dont catch exceptions
- Should "warn" users that it could throw
- But it wont catch!
- Should list such exceptionsdouble
safeDivide(int top, int bottom) throw
(DividebyZero) - Called "exception specification" or "throw list"
- Should be same in declaration and definition
- All types listed handled "normally"
- If no throw list ? all exception types considered
there - WARNING some compilers do not process the throw
list
36Throw List
- If exception thrown in function NOT inthrow
list - No errors (compile or run-time)
- Function unexpected() automatically called
- Default behavior is to terminate
- Can modify behavior
- Same result if no catch-block found
37Throw List Summary
- void someFunction() throw(DividebyZero,
OtherException) - Exception types DividebyZero or OtherException
treated normally. All others invoke unexpected() - void someFunction() throw ()
- Empty exception list. The function is not allowed
to throw any exception. Thus, all exceptions
thrown by the function invoke unexpected() - void someFunction()
- All exceptions of all types treated normally
using a catch block, i.e. behaves as with a
throw() containing all possible kinds of
exceptions
38Derived Classes
- Remember derived class objects are alsoobjects
of base class - ConsiderD is derived class of B
- If B is in exception specification ?
- Class D thrown objects will also be
treatednormally, since its also object of class
B - Note does not do automatic type cast
- double will not account for throwing an int
39unexpected()
- Default action terminates program
- No special includes or using directives
- Normally no need to redefine
- But you can
- Use set_unexpected
- Consult compiler manual or advancedtext for
details
40When to Throw Exceptions
- Typical to separate throws and catches in
separate functions - Throwing function
- Include throw statements in definition
- List exceptions in throw list
- In both declaration and definition
- Catching function
- Different function, perhaps even in different file
41Preferred throw-catch Triad throw
- void functionA() throw(MyException) throw
MyException(arg) - Function throws exception as needed
42Preferred throw-catch Triad catch
- Then some other functionvoid
functionB() try functionA()
catch (MyException e) // Handle exception
43Uncaught Exceptions
- Should catch every exception thrown
- If not ? program terminates
- terminate() is called
- Recall for functions
- If exception not in throw list unexpected()is
called - It in turn calls terminate()
- So same result
44Overuse of Exceptions
- Exceptions alter flow of control
- Similar to old "goto" construct
- "Unrestricted" flow of control
- Should be used sparingly
- Good rule
- If desire a "throw" consider how to
writeprogram without throw - If alternative reasonable ? do it
45Exception Class Hierarchies
- Useful to have considerDivideByZero class
derives from ArithmeticError exception class - All catch-blocks for ArithmeticError also catch
DivideByZero - If ArithmeticError in throw list,
thenDividebyZero also considered there
46Testing Available Memory
- new operator throws bad_alloc exceptionif
insufficient memorytry NodePtr pointer
new Nodecatch (bad_alloc) cout ltlt "Ran out
of memory!" // Can do other things here as
well - In library ltnewgt, std namespaceinclude
ltnewgtusing stdbad_alloc
47Rethrowing an Exception
- Legal to throw an exception IN catch-block!
- Typically only in rare cases
- Can even have a catch block re-throw its
exception - throw Throws same exception again
48Summary 1
- Exception handling allows separation of"normal"
cases and "exceptional" cases - Exceptions thrown in try-block
- Or within a function whose call is in try-block
- Exceptions caught in catch-block
- try-blocks typically followed by more thanone
catch-block - List more specific exceptions first
49Summary 2
- Best used with separate functions
- Especially considering callers might handle
differently - Exceptions thrown in but not caught infunction,
should be listed in throw list - Exceptions thrown but never caught ?program
terminates - Resist overuse of exceptions
- Unrestricted flow of control
50References
- Walter Savitch, Absolute C, 3rd Edition,
Addison-Wesley, 2007. - http//www.cplusplus.com/doc/tutorial/exceptions.h
tml