Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Object-Oriented Programming

Exceptions
Reading Reference: Chapter 16

Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Exceptions
Exceptions

• Exceptions are errors or unexpected events


that occur while a program is running.

• Indicate that something unexpected has


occurred or been detected

• Exception handling allow program to deal with


the problem in a controlled manner
– Handle the event without crashing the program
Exceptions

• Previously we have done errors handling like


this
Exceptions

• Previously we have done errors handling like


this

Handling division by zero error Value 0 signal error

• C++ provides a more comprehensive method of signaling and handling


errors called exceptions.
• Errors are represented by class objects
Exceptions - Terminology

• Exception: object or value that signals an error

• Throw an exception: send a signal that an


error has occurred

• Catch/Handle an exception: Handle or process


the exception;
– interpret the signal
The Exception handling
Exception Example

the function terminates on throw

How we will handle the exception? See next slide


Exceptions – Key Words

• throw – followed by an argument, is used to throw


an exception

• try – followed by a block { }, is used to invoke


code that throws an exception

• catch – followed by a block { }, is used to detect


and process exceptions thrown in preceding try
block.
– Takes a parameter that matches the type thrown.
Dealing with Exception in C++

• The syntax
Exceptions – Flow of Control

1) A function that throws an exception is called from within a


try block

2) If the function throws an exception, the function


terminates and the try block is immediately exited.
– A catch block to process the exception is searched for in the source
code immediately following the try block.

3) If a catch block is found that matches the exception


thrown, it is executed.
– If no catch block that matches the exception is found, the program
terminates i.e. the program crashes.
Program 16-1
Exceptions – Example (2)
// function that throws an exception
int totalDays(int days, int weeks)
{
if ((days < 0) || (days > 7))
throw "invalid number of days";
// the argument to throw is the
// character string
else
return (7 * weeks + days);
}
Exceptions – Example (2)
try // block that calls function
{
totDays = totalDays(days, weeks);
cout << "Total days: " << days;
}
catch (char *msg) // interpret
// exception
{
cout << "Error: " << msg;
}
Exceptions – Example (2)
– What Happens

1) try block is entered. totalDays function is called

2) If 1st parameter is between 0 and 7, total number of days is


returned and catch block is skipped over (no exception
thrown)

3) If exception is thrown, function and try block are exited,


catch blocks are scanned for 1st one that matches the data
type of the thrown exception. catch block executes
What’s covered So far…

Exceptions
• Exceptions - Terminology & keywords
– Exception: Object/value that signal an exception
– Throw an exception: throw the exception
– Catch an exception: catch{} after try{}
• Exceptions – Flow of Control
• Exception Example: Divide by Zero
• Exception Not Caught?
– Program terminate
Catching all exceptions
- general Catch
• Three dots …
indicate variable
no of args
Object Oriented Approach to
Exception Handling
Creating classes for exceptions

• An exception class can be defined in a class


can be defined for representing an exception
– Objects of exception class can be thrown to signal
the erroneous condition
• An exception class may have:
– no members: used only to signal an error
– members: pass error data to catch block
• Exception class can be defined inside the class
itself; i.e. Exception class is owned by class
– A class can have more than one exception class
NegativeSize class is declared
inside the Rectangle class, we have
to fully qualify the class name with the
scope resolution operator.

• catch statement only needs to


specify the type of exception it
handles.

• Don’t need to define a parameter of


the NegativeSize class if
processing of such an object is not
required
Program 16-2 (Continued)
Multiple Exceptions

• The programs we have studied so far test only for a


single type of error and throw only a single type of
exception.
• In many cases a program will need to test for several
different types of errors and signal which one has
occurred
– Example: Two types of exceptions in Rectangle class
Part of code skipped
Part of code skipped
Using Exception Handlers to Recover
from Errors

Part of code skipped


Extracting Data from the Exception Class

• Want an exception object to pass data back to


the exception handler.
– E.g. make Rectangle class so that it not only signal
when a negative value has been given, but also to
pass the value back.
• To do this define members in exception class that store
the data
Need to use a
parameter receiving the
exception class object
in this case

Part of code skipped


Topics Covered
Exceptions
• General catch: catch(…)
• Object Oriented Approach to Exception Handling
– Creating classes for exceptions
• Example: NegativeSize inner class inside Rectangle
– Multiple Exceptions in a class
• Example: NegativeWidth, NegativeLength inner classes in
Rectangle
– Extracting Data from the Exception Class
• member variables of Exception class store data about the
exception
• Data can be passed to catcher

You might also like