Exception Handling in Cpp-Object Oriented Programming and Data Structures-Lecture Slides, Slides of Object Oriented Programming

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

2011/2012

Uploaded on 07/13/2012

shuja
shuja 🇮🇳

4.3

(6)

51 documents

1 / 38

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ExceptionHandlinginC++ 1
ExceptionHandlinginC++
ReferenceBooks:TheCProgrammingLanguage,2nd edition,byKernighanandRitchie,
C:HowtoProgram,5th and6th editions,byDeitel andDeitel,
TheC++ProgrammingLanguage,3rd edition,byBjarne Stroustrup
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download Exception Handling in Cpp-Object Oriented Programming and Data Structures-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Exception^ Handling^ in^ C++^

1

Exception

Handling

in^ C++

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

Outline

-^ 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

Exception

Handling

in^ C++

-^ A^ standard

mechanism

for^ processing

errors

-^ Especially^ important

when^ working

on^ a^ project with^ a^ large^ team

of^ programmers

-^ C++^ exception

handling^ is

much^ like^ Java’s

-^ Java’s^ exception

handling^ is

much^ like^ C

++ docsity.com

Exception^ Handling^ in^ C++^

5

Fundamental

Philosophy

-^ Mechanism

for^ sending

an^ exception

signal^ up

the^ call^ stack^ •^ Regardless

of^ intervening

calls

Exception^ Handling^ in^ C++^

7

Fundamental

Philosophy

(continued)

-^ Remove^ error

‐handling^ code

from^ the

program^ execution’s

“main^ line”

-^ Programmers

can^ handle

any^ exceptions

they

choose^ –^ All^ exceptions^ –^ All^ exceptions

of^ a^ certain^ type

-^ All^ exceptions

of^ a^ group^ of

related^ types

Exception^ Handling^ in^ C++^

8

Fundamental

Philosophy

(continued)

-^ Programs

can – Recover from^ exceptions – Hide exceptions – Pass exceptions^ up^ the

“chain^ of^ command”

-^ Ignore^ certain

exceptions^ and

let^ someone

else

handle^ them

Exception^ Handling^ in^ C++^

10

Class^ exception

-^ The^ standard

C++^ base^ class

for^ all^ exceptions

-^ Provides^

derived^ classes

with^ virtual

function

what^ –^ Returns^ the

exception’s^ stored

error^ message

Exception^ Handling^ in^ C++^

11

Fig.^ 27.11^ |^

Standard

Library^ exception classes.

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^ 5 using^ std::cin; 6 using^ std::cout; 7 using^ std::endl; 8 9 #include^ "DivideByZeroException.h" //

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 &divideByZeroException

)

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

Software

Engineering

Observation

-^ Exceptions

may^ surface – through explicitly^ mentioned

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

Catch^ Handlers

-^ 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

Common

Programming

Errors

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

Fundamental

Philosophy

(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