






























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture was delivered by Chanky Rathin for Object Oriented Programming and Data Structures at Anna University of Technology. It includes: C , Templates, Exceptions, Function, Class, Handler, Overloading, Instantiating, Naïve, Approach, Parameter, Lists
Typology: Slides
1 / 38
This page cannot be seen from the preview
Don't miss anything!































Exception^ Handling^ in^ C++^
1
Reference^ Books:^ The^ C^ Programming
nd^ Language , 2 edition,^ by^ Kernighan and^ Ritchie, th^ th C: How to Program , 5 and^6 editions,^ by^ Deitel and^ Deitel, The^ C++^ Programming^ Language
rd^ , 3 edition,^ by^ Bjarne Stroustrup
Exception^ Handling^ in^ C++^
2
-^ What^ exceptions
are^ and^ when
to^ use^ them
-^ Using^ try
,^ catch^ and
throw^ to^ detect,
handle^ and
indicate^ exceptions,
respectively
-^ To^ process
uncaught^ and
unexpected^
exceptions
-^ To^ declare
new^ exception
classes
-^ How^ stack
unwinding^ enables
exceptions^ not
caught
in^ one^ scope
to^ be^ caught
in^ another^ scope
-^ To^ handle^
new^ failures
-^ To^ understand
the^ standard
exception^ hierarchy
Exception^ Handling^ in^ C++^
4
-^ A^ standard
-^ Especially^ important
when^ working
on^ a^ project with^ a^ large^ team
of^ programmers
-^ C++^ exception
-^ Java’s^ exception
Exception^ Handling^ in^ C++^
5
-^ Mechanism
of^ intervening
calls
Exception^ Handling^ in^ C++^
7
(continued)
-^ Remove^ error
-^ Programmers
of^ a^ certain^ type
-^ All^ exceptions
of^ a^ group^ of
related^ types
Exception^ Handling^ in^ C++^
8
(continued)
-^ Programs
“chain^ of^ command”
-^ Ignore^ certain
exceptions^ and
let^ someone
else
handle^ them
Exception^ Handling^ in^ C++^
10
-^ The^ standard
-^ Provides^
what^ –^ Returns^ the
exception’s^ stored
error^ message
Exception^ Handling^ in^ C++^
11
Exception^ Handling^ in^ C++^
13
1 // Fig. 27.2: Fig27_02.cpp 2 // A^ simple^ exception-handling
example^ that^ checks
for
3 // divide-by-zero
exceptions. 4 #include^
DivideByZeroException
class
1011 // perform division
and throw^ DivideByZeroException
object^ if
12 // divide-by-zero
exception^ occurs 13 double^ quotient(
int^ numerator, int
denominator^ ) 14 { 15 //^ throw^ DivideByZeroException
if^ trying^ to^ divide
by^ zero
16 if^ ( denominator
==^0 ) 17 throw^ DivideByZeroException();
// terminate function 1819 //^ return^ division
result 20 return^ static_cast<
double^ >(^ numerator
) /^ denominator; 21 }^ //^ end^ function
quotient 2223 int^ main() 24 { 25 int^ number1;
// user-specified
numerator 26 int^ number2;
// user-specified
denominator 27 double^ result;
//^ result^ of^ division 2829 cout << "Enter
two^ integers^ (end-of-file
to^ end): ";
Zero DivideExample •^ Fig27-2^ – (1 of 2) docsity.com
Exception^ Handling^ in^ C++^
14
3031 //^ enable^ user to enter
two^ integers to divide 32 while ( cin
number1^ >> number
) 33 { 34 //^ try^ block
contains code that
might^ throw exception 35 //^ and^ code that
should not execute if an
exception occurs 36 try 37 { 38 result
= quotient( number1, number
); 39 cout << "The quotient
is: " << result << endl; 40 } // end try 4142 //^ exception
handler^ handles a
divide-by-zero^ exception 43 catch^ ( DivideByZeroException ÷ByZeroException
)
44 { 45 cout << "Exception occurred:
" 46 << divideByZeroException.what()
<< endl; 47 } // end catch 4849 cout << "\nEnter
two^ integers (end-of-file
to^ end): ";
50 }^ //^ end^ while 5152 cout^ << endl; 53 return 0;^ //
terminate^ normally 54 }^ //^ end^ main
Zero DivideExample •^ Fig27-2^ – (2 of 2) docsity.com
Exception^ Handling^ in^ C++^
CS‐2303,^ C‐Term^2010
16
-^ Exceptions
code^ in^ a^ try
block,
-^ through^ calls
to^ other^ functions
and
-^ through^ deeply
nested^ function
calls^ initiated
by
code^ in^ a^ try
block.
Exception^ Handling^ in^ C++^
17
-^ Immediately
follow^ a^ try
block
-^ One^ or^ more
catch^ handlers
for^ each^ try^ block
-^ Keyword^ catch •^ Exception^
parameter^ enclosed
in^ parentheses
-^ Represents^ the
type^ of^ exception
to^ process
-^ Can^ provide^
an^ optional^ parameter
name^ to^ interact
with
the^ caught^ exception
object
-^ Executes^ if
exception^ parameter
type^ matches
the
exception^ thrown
in^ the^ try^ block
-^ Could^ be^ a^ base
class^ of^ the^ thrown
exception’s^ class
Exception^ Handling^ in^ C++^
19
Syntax^ error^
to^ place^ code
between^ a^ try
block^ and
its^ corresponding
catch^ handlers Each^ catch^ handler
can^ have^ only
a^ single^ parameter
-^ Specifying^ a^ comma
‐separated^ list^ of^ exception^ parameters
is^ a
syntax^ error • Logic error^ to^ catch
the^ same^ type
in^ two^ different catch^ handlers
following^ a^ single
try^ block
Exception^ Handling^ in^ C++^
20
(continued)
-^ Termination
model^ of^ exception
handling
-^ try^ block^ expires
when^ an^ exception
occurs
-^ Local^ variables^ in^ try^ block^ go^ out
of^ scope
-^ Code^ within^
the^ matching^ catch
handler^ executes
-^ Control^ resumes
with^ the^ first^ statement
after^ the^ last catch^ handler^ following
the^ try^ block
-^ Stack^ unwinding^ –^ Occurs^ if
no^ matching^ catch
handler^ is^ found
-^ Program^ attempts
to^ locate^ another
enclosing^ try
block
in^ the^ calling^ function
Control^ does^ not
return^ to^ throw
point docsity.com