0% found this document useful (0 votes)
22 views

Exception Handling

Uploaded by

Manozer Mensah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Exception Handling

Uploaded by

Manozer Mensah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Exception Handling

Akyana Britwum (PhD)


Exception Handling Basics

• Defining exception classes


• Multiple throws and catches
Learning • Exception specifications

Objectives Programming Techniques for


Exception Handling
• When to throw exceptions
• Exception class hierarchies
Typical approach to development:
• Write programs assuming things go as
planned
• Get "core" working
• Then take care of "exceptional" cases
Introduction C++ exception-handling facilities
• Handle "exceptional" situations
• Mechanism "signals" unusual happening
• Another place in code "deals" with
exception
Meant to be used "sparingly"
• In "involved" situations
Exception- Difficult to teach such large examples
Handling Approach:
Basics • Simple "toy" examples, that would not
normally use exception-handling
• Keep in mind "big picture"
Toy Example see main code
• Imagine: people must always eat their donuts with at least on cup of
icycup yoghurt.
cout << "Enter number of donuts:";
cin >> donuts;
cout << "Enter number of cups of icycup:>:";
cin >> icycup;
dpg = donuts/static_cast<double>(icycup);
cout << donuts << "donuts.\n";
<< milk << "cups of icycup.\n";
<< "You have " << dpg
<< "ddonuts for each cup of icycup.\n";
• Basic code assumes never run out of icycup
Notice: If no icycup→divide by zero
error!

Program should accommodate unlikely


Toy situation of running out of milk

Example if- • Can use simple if-else structure:


if (icycup <= 0)
cout << "Please buy icycup to go with
else those fine donuts!!!\n";
else
{…}

Notice: no exception-handling here


Code between keywords try and
catch

Toy • Same code from ordinary version, except


if statement simpler:
if (icycup <= 0)
Example throw donuts;
• Much cleaner code
Discussion • If "no icycup" → do something exceptional

The "something exceptional" is


provided
after keyword catch
Try block

• Handles "normal" situation


Toy Catch block
Example • Handles "exceptional" situations
try-catch Provides separation of normal
from exceptional
• Not big deal for this simple example, but
important concept
Basic method of exception-handling is
try-throw-catch

try block Try block:


try
{
Some_Code;
}
• Contains code for basic algorithm when all
goes smoothly
• Inside try-block, when something
unusual happens:
try
{
Code_To_Try
throw if (exceptional_happened)
throw donuts;
More_Code
}
• Keyword throw followed by exception type
• Called "throwing an exception"
catch-block
• When something thrown → goes somewhere
• In C++, flow of control goes from try-block to
catch-block
• try-block is "exited" and control passes to catch-block
• Executing catch block called "catching the
exception"
• Exceptions must be "handled" in some
catch block
catch-block More
• Recall:
catch(int x)
{
cout << e << " donuts and no cups of icycup!\n";
<< " Go and buy some ICYCUP!!!\n";
}
• Looks like function definition with
int parameter!
• Not a function, but works similarly
• Throw like "function call"
Recall: catch(int x)

“x" called catch-block parameter


• Each catch block can have at most ONE
catch-block catch-block parameter
Does two things:
Parameter • type name specifies what kind of
thrown
value the catch-block can catch
• Provides name for thrown value caught;
can "do things" with value
throw statement can throw value of
any type

Defining Exception class


Exception • Contains objects with information to

Classes be thrown
• Can have different types identifying each
possible exceptional situation
• Still just a class
• An "exception class" due to how it’s used
Consider:
class NoIcycup
{
public:

Exception
NoIcycup() { }
NoIcycup(int howMany) : count(howMany) { }
int getcount() const { return count; }
private:

Class for
int count;
};

Toy
Example throw NoIcycup(donuts);

• Invokes constructor of NoIcycup class


try-block typically throws any number of
exception values, of differing types

Multiple Of course only one exception thrown

Throws and • Since throw statement ends try-block

Catches But different types can be thrown

• Each catch block only catches "one type"


• Typical to place many catch-blocks after each
try-block
• To catch "all-possible" exceptions to be thrown
01 02 03
Catching
Order of catch blocks Catch-blocks tried "in order" Consider:
important after try-block catch (…) { }
•First match handles it! •Called "catch-all", "default"
exception handler
•Catches any exception
•Ensure catch-all placed AFTER more
specific
exceptions!
•Or others will never be caught!
Consider:
class DivideByZero
{}

Trivial No member variables

Exception No member functions (except default


constructor)

Classes Nothing but it’s name, which is enough

• Might be "nothing to do" with exception value


• Used simply to "get to" catch block
• Can omit catch block parameter
Function might throw exception

Callers might have different


Throwing "reactions"
Exception • Some might desire to "end program"
• Some might continue, or do something else
in Function Makes sense to "catch" exception in
calling function’s try-catch-block
• Place call inside try-block
• Handle in catch-block after try-block
Consider:
try
{

Throwing }
quotient = safeDivide(num, den);

catch (DivideByZero)

Exception {…}

in Function
safeDivide() function throws DividebyZero
Example exception

• Handled back in caller’s catch-block


Functions that don’t catch exceptions

• Should "warn" users that it could throw

Exception • But it won’t catch!

Should list such exceptions:


Specification double safeDivide(int top, int bottom)
throw (DividebyZero);
• Called "exception specification" or "throw list"
• Should be in declaration and definition
• All types listed handled "normally"
• If no throw list → all types considered there
If exception thrown in function
NOT in
throw list:
• No errors (compile or run-time)
• Function unexpected() automatically called
Throw List • Default behavior is to terminate
• Can modify behavior

Same result if no catch-block found


Throw List Summary
• void someFunction()
throw(DividebyZero, OtherException);
//Exception types DividebyZero or OtherException
//treated normally. All others invoke unexpected()
• void someFunction() throw ();
//Empty exception list, all exceptions invoke
unexpected()
• void someFunction();
//All exceptions of all types treated normally
Remember: derived class objects also
objects of base class

Consider:
D is derived class of B
Derived If B is in exception specification →
Classes • Class D thrown objects will also be treated
normally, since it’s also object of class B

Note: does not do automatic type cast:

• double will not account for throwing an int


Default action: terminates program
• No special includes or using directives

Normally no need to redefine


unexpected()
But you can:
• Use set_unexpected
• Consult compiler manual or advanced
text for details
Typical to separate throws and catches

• In separate functions

When to Throwing function:

Throw • Include throw statements in definition


• List exceptions in throw list

Exceptions • In both declaration and definition

Catching function:

• Different function, perhaps even in different file


Preferred throw-catch Triad: throw

void functionA() throw


(MyException)
{
… Function throws exception as
throw MyException(arg); needed

}
Preferred throw-catch Triad: catch

• Then some other function:


void functionB()
{

try
{

functionA();

}
catch (MyException e)
{ // Handle exception
}

}
Should catch every exception thrown

If not → program terminates

Uncaught • terminate() is called

Recall for functions


Exceptions • If exception not in throw list: unexpected()
is called
• It in turn calls terminate()

So same result
Exceptions alter flow of control
• Similar to old "goto" construct
• "Unrestricted" flow of control

Overuse of Should be used sparingly

Exceptions Good rule:


• If desire a "throw": consider how to
write
program without throw
• If alternative reasonable → do it
• Useful to have; consider:
DivideByZero class derives from:
Exception ArithmeticError exception class
Class • All catch-blocks for ArithmeticError also
catch DivideByZero
Hierarchies • If ArithmeticError in throw list, then
DividebyZero also considered there
Testing Available Memory
• new operator throws bad_alloc exception
if insufficient memory:
try
{
NodePtr pointer = new Node;
}
catch (bad_alloc)
{
cout << "Ran out of memory!";
// Can do other things here as well…
}
• In library <new>, std namespace
Legal to throw exception IN catch-block!

• Typically only in rare cases

Rethrowing Throws to catch-block "farther up chain"

an Can re-throw same or new exception

Exception • rethrow;
• Throws same exception again
• throw newExceptionUp;
• Throws new exception to next catch-block
Exception handling
allows separation of
"normal" cases and
"exceptional" cases

Exceptions thrown • Or within a function


in try-block whose call is in try-block
Summary 1
Exceptions caught
in catch-block

try-blocks typically
followed by more • List more specific
than exceptions first
one catch-block
• Especially considering callers
Best used with
might
separate functions
handle differently

Exceptions thrown
in but not caught in
function, should be
listed in throw list
Summary 2 Exceptions thrown
but never caught →
program terminates

Resist overuse of
• Unrestricted flow of control
exceptions

You might also like