SlideShare a Scribd company logo
Chapter 14:
Exception Handling
Objectives
• In this chapter, you will:
– Learn what an exception is
– Learn how to handle exceptions within a program
– Learn how a try/catch block is used to handle
exceptions
– Learn how to throw an exception
– Become familiar with C++ exception classes and
how to use them in a program
2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Objectives (cont’d.)
– Learn how to create your own exception classes
– Discover how to throw and rethrow an exception
– Explore exception handling techniques
– Explore stack unwinding
3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Introduction
• Exception: undesirable event detectable
during program execution
• Code to handle exceptions depends on the
type of application being developed
• May or may not want the program to terminate
when an exception occurs
• Can add exception-handling code at point
where an error can occur
4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Handling Exceptions Within a
Program
• Assert function:
– Checks if an expression meets certain condition(s)
– If conditions are not met, it terminates the
program
• Example: division by 0
– If divisor is zero, assert terminates the program
with an error message
5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
C++ Mechanisms
of Exception Handling
• try/catch block: used to handle exceptions
• Exception must be thrown in a try block and
caught by a catch block
• C++ provides support to handle exceptions via
a hierarchy of classes
6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
try/catch Block
• Statements that may generate an exception
are placed in a try block
• The try block also contains statements that
should not be executed if an exception occurs
• try block is followed by one or more catch
blocks
7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
try/catch Block (cont’d.)
8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
try/catch Block (cont’d.)
• catch block:
– Specifies the type of exception it can catch
– Contains an exception handler
• If the heading of a catch block contains ...
(ellipses) in place of parameters
– Block can catch exceptions of all types
• If no exception is thrown in a try block
– All catch blocks are ignored
– Execution resumes after the last catch block
9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
try/catch Block (cont’d.)
• If an exception is thrown in a try block
– Remaining statements (in block) are ignored
• Program searches catch blocks in order,
looking for an appropriate exception handler
– If the type of thrown exception matches the
parameter type in one of the catch blocks:
• Code of that catch block executes
• Remaining catch blocks are ignored
10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
try/catch Block (cont’d.)
• A catch block can have at most one catch
block parameter
– catch block parameter becomes a placeholder
for the value thrown
11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Throwing an Exception
• For try/catch to work, the exception
must be thrown in the try block
• General syntax:
throw exception;
where expression is a constant value,
variable, or object
• Object being thrown can be a specific object
or an anonymous object
• In C++, an exception is a value
12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Order of catch Blocks
• catch block can catch:
– All exceptions of a specific type
– All types of exceptions
• A catch block with an ellipsis (. . .) catches
any type of exception
– If used, it should be the last catch block of
that sequence
• Be careful about the order in which you list
catch blocks
13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Using C++ Exception Classes
• C++ provides support to handle exceptions via
a hierarchy of classes
• what function: returns a string containing the
exception object thrown by C++’s built-in
exception classes
• class exception: base class of the
exception classes provided by C++
– Contained in the header file exception
14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Using C++ Exception Classes (cont’d.)
• Two subclasses of exception (defined in
stdexcept):
– logic_error includes subclasses:
• invalid_argument: for use when illegal
arguments are used in a function call
• out_of_range: string subscript out of range error
• length_error: if a length greater than the
maximum allowed for a string object is used
– runtime_error includes subclasses:
• overflow_error and underflow_error
15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Creating Your Own Exception Classes
• Can create your own exception classes to
handle specific exceptions
– C++ uses the same mechanism to process these
exceptions
• throw statement: used to throw your own
exceptions
• Any class can be an exception class
– How you use the class makes it an exception class
16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Creating Your Own Exception Classes
(cont’d.)
• Exception class with member variables
typically includes:
– Constructors
– The function what
17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Rethrowing and Throwing an
Exception
• When an exception occurs in a try block,
control immediately passes to one of the
catch blocks, which either:
– Handles the exception, or partially processes the
exception, then rethrows the same exception
– Rethrows another exception for the calling
environment to handle
• This allows you to provide exception-
handling code all in one place
18C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Rethrowing and Throwing an
Exception (cont’d.)
• Syntax to rethrow an exception caught by a
catch block:
– If the same exception is to be rethrown:
– If a different exception is to be thrown
where expression is a constant value,
variable, or object
19C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Rethrowing and Throwing an
Exception (cont’d.)
• Object being thrown can be:
– A specific object
– An anonymous object
• A function specifies the exceptions it throws in
its heading using the throw clause
20C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Exception-Handling Techniques
• When an exception occurs, the programmer
usually has three choices:
– Terminate the program
– Include code to recover from the exception
– Log the error and continue
21C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Terminate the Program
• In some cases, it is best to terminate the
program when an exception occurs
• Example: if an input file does not exist when
the program executes
– There is no point in continuing with the program
– Program can output an appropriate error message
and terminate
22C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Fix the Error and Continue
• In some cases, you will want to handle the
exception and let the program continue
• Example: a user inputs a letter instead of a
number
– The input stream will enter the fail state
– Can include the necessary code to keep prompting
the user to input a number until the entry is valid
23C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Log the Error and Continue
• Example: if the program is designed to run a
nuclear reactor or continuously monitor a
satellite
– It cannot be terminated if an exception occurs
• When an exception occurs
– The program should write the exception into a file
and continue to run
24C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Stack Unwinding
• When an exception is thrown in a function,
the function can do the following:
– Do nothing
– Partially process the exception and throw the
same exception or a new exception
– Throw a new exception
• In each case, the function-call stack is
unwound so that the exception can be caught
in the next try/catch block
25C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Stack Unwinding (cont’d.)
• When the function call stack is unwound:
– The function in which the exception was not
caught and/or rethrown terminates
– Memory for its local variables is destroyed
• Stack unwinding continues until:
– A try/catch handles the exception, or
– The program does not handle the exception
• The function terminate is called to terminate
the program
26C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary
• Exception: an undesirable event detectable
during program execution
• assert checks whether an expression
meets a specified condition; terminates if
not met
• try/catch block handles exceptions
• Statements that may generate an
exception are placed in a try block
27C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• catch block specifies type of exception it
can catch and contains an exception handler
• If no exceptions are thrown in a try block,
all catch blocks for that try block are
ignored
• Data type of catch block parameter
specifies type of exception that catch block
can catch
28C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• exception: base class for exception
classes
• what function: returns a string containing
the exception object thrown by built-in
exception classes
• You can create your own exception classes
• A function specifies the exceptions it throws
in its heading with the throw clause
29C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• If the program does not handle the
exception, then the function terminate
terminates the program
• Any class can be considered an exception
class, based on how it is used
30C++ Programming: From Problem Analysis to Program Design, Seventh Edition

More Related Content

What's hot

9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15
Terry Yoast
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
Terry Yoast
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
Terry Yoast
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16
Terry Yoast
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
Terry Yoast
 
9781439035665 ppt ch05
9781439035665 ppt ch059781439035665 ppt ch05
9781439035665 ppt ch05Terry Yoast
 
9781111530532 ppt ch11
9781111530532 ppt ch119781111530532 ppt ch11
9781111530532 ppt ch11Terry Yoast
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05Terry Yoast
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
MLG College of Learning, Inc
 
TMPA-2017: Vellvm - Verifying the LLVM
TMPA-2017: Vellvm - Verifying the LLVMTMPA-2017: Vellvm - Verifying the LLVM
TMPA-2017: Vellvm - Verifying the LLVM
Iosif Itkin
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
baran19901990
 
Pptchapter04
Pptchapter04Pptchapter04
Pptchapter04
Richard Styner
 
C++ ch1
C++ ch1C++ ch1
150412 38 beamer methods of binary analysis
150412 38 beamer methods of  binary analysis150412 38 beamer methods of  binary analysis
150412 38 beamer methods of binary analysis
Raghu Palakodety
 
Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error predictionNIKHIL NAWATHE
 
TMPA-2017: Evolutionary Algorithms in Test Generation for digital systems
TMPA-2017: Evolutionary Algorithms in Test Generation for digital systemsTMPA-2017: Evolutionary Algorithms in Test Generation for digital systems
TMPA-2017: Evolutionary Algorithms in Test Generation for digital systems
Iosif Itkin
 

What's hot (18)

9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
9781439035665 ppt ch05
9781439035665 ppt ch059781439035665 ppt ch05
9781439035665 ppt ch05
 
9781111530532 ppt ch11
9781111530532 ppt ch119781111530532 ppt ch11
9781111530532 ppt ch11
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
Chap05
Chap05Chap05
Chap05
 
TMPA-2017: Vellvm - Verifying the LLVM
TMPA-2017: Vellvm - Verifying the LLVMTMPA-2017: Vellvm - Verifying the LLVM
TMPA-2017: Vellvm - Verifying the LLVM
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
 
Pptchapter04
Pptchapter04Pptchapter04
Pptchapter04
 
ETAPS03 SC.ppt
ETAPS03 SC.pptETAPS03 SC.ppt
ETAPS03 SC.ppt
 
C++ ch1
C++ ch1C++ ch1
C++ ch1
 
150412 38 beamer methods of binary analysis
150412 38 beamer methods of  binary analysis150412 38 beamer methods of  binary analysis
150412 38 beamer methods of binary analysis
 
Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error prediction
 
TMPA-2017: Evolutionary Algorithms in Test Generation for digital systems
TMPA-2017: Evolutionary Algorithms in Test Generation for digital systemsTMPA-2017: Evolutionary Algorithms in Test Generation for digital systems
TMPA-2017: Evolutionary Algorithms in Test Generation for digital systems
 

Viewers also liked

Exception handling
Exception handlingException handling
Exception handling
Prafull Johri
 
Exception handling
Exception handlingException handling
Exception handling
Abhishek Pachisia
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
Deepak Tathe
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templatesfarhan amjad
 
Exception handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaException handling in c++ by manoj vasava
Exception handling in c++ by manoj vasava
Manoj_vasava
 
C++ ala
C++ alaC++ ala
C++ ala
Megha Patel
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
What is Exception Handling?
What is Exception Handling?What is Exception Handling?
What is Exception Handling?
Syed Bahadur Shah
 
Software development
Software developmentSoftware development
Software development
Terry Yoast
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
Terry Yoast
 
Exception handler
Exception handler Exception handler
Exception handler dishni
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15Kumar
 
Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]
ppd1961
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04Terry Yoast
 
Computer science education week or csed week
Computer science education week or csed weekComputer science education week or csed week
Computer science education week or csed week
Terry Yoast
 

Viewers also liked (18)

Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
Exception handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaException handling in c++ by manoj vasava
Exception handling in c++ by manoj vasava
 
C++ ala
C++ alaC++ ala
C++ ala
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
What is Exception Handling?
What is Exception Handling?What is Exception Handling?
What is Exception Handling?
 
Software development
Software developmentSoftware development
Software development
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
Exception handler
Exception handler Exception handler
Exception handler
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
 
Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04
 
Optbook
OptbookOptbook
Optbook
 
Computer science education week or csed week
Computer science education week or csed weekComputer science education week or csed week
Computer science education week or csed week
 
Savitch ch 05
Savitch ch 05Savitch ch 05
Savitch ch 05
 
Chapter 05
Chapter 05Chapter 05
Chapter 05
 

Similar to 9781285852744 ppt ch14

Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptx
VishuSaini22
 
Exception hierarchy
Exception hierarchyException hierarchy
Exception hierarchy
Ashfaaq Mahroof
 
Lecture 3.1.1 Try Throw Catch.pptx
Lecture 3.1.1 Try Throw Catch.pptxLecture 3.1.1 Try Throw Catch.pptx
Lecture 3.1.1 Try Throw Catch.pptx
sunilsoni446112
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
Exception handling in .net
Exception handling in .netException handling in .net
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
lecture-c-corr-effkkkkkkkkkkkkkp (1).pptlecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
ZeeshanAli593762
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
Alpesh Oza
 
UNIT III (2).ppt
UNIT III (2).pptUNIT III (2).ppt
UNIT III (2).ppt
VGaneshKarthikeyan
 
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.ppt
Ajit Mali
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
Bharath K
 
Week7_ExceptionHandling.ppt
Week7_ExceptionHandling.pptWeek7_ExceptionHandling.ppt
Week7_ExceptionHandling.ppt
SatyaKumari18
 
Unit iii
Unit iiiUnit iii
Unit iii
snehaarao19
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javapriyankazope
 
Exception handling
Exception handlingException handling
Exception handling
pooja kumari
 
43c
43c43c

Similar to 9781285852744 ppt ch14 (20)

Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptx
 
Exception hierarchy
Exception hierarchyException hierarchy
Exception hierarchy
 
Lecture 3.1.1 Try Throw Catch.pptx
Lecture 3.1.1 Try Throw Catch.pptxLecture 3.1.1 Try Throw Catch.pptx
Lecture 3.1.1 Try Throw Catch.pptx
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
 
Exception handling in .net
Exception handling in .netException handling in .net
Exception handling in .net
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
 
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
lecture-c-corr-effkkkkkkkkkkkkkp (1).pptlecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
UNIT III (2).ppt
UNIT III (2).pptUNIT III (2).ppt
UNIT III (2).ppt
 
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.ppt
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Unit iii pds
Unit iii pdsUnit iii pds
Unit iii pds
 
Week7_ExceptionHandling.ppt
Week7_ExceptionHandling.pptWeek7_ExceptionHandling.ppt
Week7_ExceptionHandling.ppt
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Java exeception handling
Java exeception handlingJava exeception handling
Java exeception handling
 
43c
43c43c
43c
 

More from Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
Terry Yoast
 

More from Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

Recently uploaded

Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 

Recently uploaded (20)

Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 

9781285852744 ppt ch14

  • 2. Objectives • In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program – Learn how a try/catch block is used to handle exceptions – Learn how to throw an exception – Become familiar with C++ exception classes and how to use them in a program 2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 3. Objectives (cont’d.) – Learn how to create your own exception classes – Discover how to throw and rethrow an exception – Explore exception handling techniques – Explore stack unwinding 3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 4. Introduction • Exception: undesirable event detectable during program execution • Code to handle exceptions depends on the type of application being developed • May or may not want the program to terminate when an exception occurs • Can add exception-handling code at point where an error can occur 4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 5. Handling Exceptions Within a Program • Assert function: – Checks if an expression meets certain condition(s) – If conditions are not met, it terminates the program • Example: division by 0 – If divisor is zero, assert terminates the program with an error message 5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 6. C++ Mechanisms of Exception Handling • try/catch block: used to handle exceptions • Exception must be thrown in a try block and caught by a catch block • C++ provides support to handle exceptions via a hierarchy of classes 6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 7. try/catch Block • Statements that may generate an exception are placed in a try block • The try block also contains statements that should not be executed if an exception occurs • try block is followed by one or more catch blocks 7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 8. try/catch Block (cont’d.) 8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 9. try/catch Block (cont’d.) • catch block: – Specifies the type of exception it can catch – Contains an exception handler • If the heading of a catch block contains ... (ellipses) in place of parameters – Block can catch exceptions of all types • If no exception is thrown in a try block – All catch blocks are ignored – Execution resumes after the last catch block 9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 10. try/catch Block (cont’d.) • If an exception is thrown in a try block – Remaining statements (in block) are ignored • Program searches catch blocks in order, looking for an appropriate exception handler – If the type of thrown exception matches the parameter type in one of the catch blocks: • Code of that catch block executes • Remaining catch blocks are ignored 10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 11. try/catch Block (cont’d.) • A catch block can have at most one catch block parameter – catch block parameter becomes a placeholder for the value thrown 11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 12. Throwing an Exception • For try/catch to work, the exception must be thrown in the try block • General syntax: throw exception; where expression is a constant value, variable, or object • Object being thrown can be a specific object or an anonymous object • In C++, an exception is a value 12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 13. Order of catch Blocks • catch block can catch: – All exceptions of a specific type – All types of exceptions • A catch block with an ellipsis (. . .) catches any type of exception – If used, it should be the last catch block of that sequence • Be careful about the order in which you list catch blocks 13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 14. Using C++ Exception Classes • C++ provides support to handle exceptions via a hierarchy of classes • what function: returns a string containing the exception object thrown by C++’s built-in exception classes • class exception: base class of the exception classes provided by C++ – Contained in the header file exception 14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 15. Using C++ Exception Classes (cont’d.) • Two subclasses of exception (defined in stdexcept): – logic_error includes subclasses: • invalid_argument: for use when illegal arguments are used in a function call • out_of_range: string subscript out of range error • length_error: if a length greater than the maximum allowed for a string object is used – runtime_error includes subclasses: • overflow_error and underflow_error 15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 16. Creating Your Own Exception Classes • Can create your own exception classes to handle specific exceptions – C++ uses the same mechanism to process these exceptions • throw statement: used to throw your own exceptions • Any class can be an exception class – How you use the class makes it an exception class 16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 17. Creating Your Own Exception Classes (cont’d.) • Exception class with member variables typically includes: – Constructors – The function what 17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 18. Rethrowing and Throwing an Exception • When an exception occurs in a try block, control immediately passes to one of the catch blocks, which either: – Handles the exception, or partially processes the exception, then rethrows the same exception – Rethrows another exception for the calling environment to handle • This allows you to provide exception- handling code all in one place 18C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 19. Rethrowing and Throwing an Exception (cont’d.) • Syntax to rethrow an exception caught by a catch block: – If the same exception is to be rethrown: – If a different exception is to be thrown where expression is a constant value, variable, or object 19C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 20. Rethrowing and Throwing an Exception (cont’d.) • Object being thrown can be: – A specific object – An anonymous object • A function specifies the exceptions it throws in its heading using the throw clause 20C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 21. Exception-Handling Techniques • When an exception occurs, the programmer usually has three choices: – Terminate the program – Include code to recover from the exception – Log the error and continue 21C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 22. Terminate the Program • In some cases, it is best to terminate the program when an exception occurs • Example: if an input file does not exist when the program executes – There is no point in continuing with the program – Program can output an appropriate error message and terminate 22C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 23. Fix the Error and Continue • In some cases, you will want to handle the exception and let the program continue • Example: a user inputs a letter instead of a number – The input stream will enter the fail state – Can include the necessary code to keep prompting the user to input a number until the entry is valid 23C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 24. Log the Error and Continue • Example: if the program is designed to run a nuclear reactor or continuously monitor a satellite – It cannot be terminated if an exception occurs • When an exception occurs – The program should write the exception into a file and continue to run 24C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 25. Stack Unwinding • When an exception is thrown in a function, the function can do the following: – Do nothing – Partially process the exception and throw the same exception or a new exception – Throw a new exception • In each case, the function-call stack is unwound so that the exception can be caught in the next try/catch block 25C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 26. Stack Unwinding (cont’d.) • When the function call stack is unwound: – The function in which the exception was not caught and/or rethrown terminates – Memory for its local variables is destroyed • Stack unwinding continues until: – A try/catch handles the exception, or – The program does not handle the exception • The function terminate is called to terminate the program 26C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 27. Summary • Exception: an undesirable event detectable during program execution • assert checks whether an expression meets a specified condition; terminates if not met • try/catch block handles exceptions • Statements that may generate an exception are placed in a try block 27C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 28. Summary (cont’d.) • catch block specifies type of exception it can catch and contains an exception handler • If no exceptions are thrown in a try block, all catch blocks for that try block are ignored • Data type of catch block parameter specifies type of exception that catch block can catch 28C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 29. Summary (cont’d.) • exception: base class for exception classes • what function: returns a string containing the exception object thrown by built-in exception classes • You can create your own exception classes • A function specifies the exceptions it throws in its heading with the throw clause 29C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 30. Summary (cont’d.) • If the program does not handle the exception, then the function terminate terminates the program • Any class can be considered an exception class, based on how it is used 30C++ Programming: From Problem Analysis to Program Design, Seventh Edition