Vous êtes sur la page 1sur 28

Contents

Definition of Exception

Exception support in programming languages Exception handling


try block

Catch block (Exception handler)


Throw clause

Exception handling with exception classes

Multiple Exception
1/22/2013 Exception 1

Definition of Exception
A condition, often an error, that causes the program or

microprocessor to branch to a different routine. The terms interrupt and exception are very close in meaning. Both can be used to refer to either hardware or software. The only real difference is that an exception usually indicates an error condition. The exception represents an error or unexpected events.

1/22/2013

Exception

Exception support in programming languages


Many computer languages, such as Actionscript, Ada,

BlitzMax, C++, C Sharp, D, ECMAScript, Eiffel, Java, ML, Object Pascal(e.g. Delphi, Free Pascal, and the like), Objective-C, Ocaml, PHP(as of version 5), PL/1, Prolog, Python, REALbasic, Ruby, Visual Prolog and most .NET languages have built-in support for exceptions and exception handling.

1/22/2013

Exception

Exception handling
Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution.

1/22/2013

Exception

Cont
In C++ exception are controlled in two ways: By compiler By using a set of blocks/clause such throw, try and catch.

1/22/2013

Exception

Cont
The exception handling mechanism is made up of the

following elements:
try blocks catch blocks throw expressions

1/22/2013

Exception

try block
All statements of C++ program which are doubt to cause

an error must be enclosed in try block. Following points must be noted for try block.
More than one try block can be used within same block. Each try block must be followed by at least one catch block

in same scope. Each try block can followed by more than one catch block.

1/22/2013

Exception

Cont
The syntax of try block is

try { Statement(s); }

1/22/2013

Exception

Catch block (Exception handler)


The catch block is used to enclosed the code which can be

used to handle an exception such code refers to exception handler. Following points must be noted for catch block:
The catch block without using a try block causes an error.
Each catch block can have only one parameter. The

parameter of catch block can be of any primitive data type or user define type such as an object of a class.

1/22/2013

Exception

Cont
When a catch block caught and its execution completed then

control will not transfer back to point from where exception is thrown. After execution of a catch block control is transferred to the next coming statement of current catch block. If next coming statement is itself is a catch block then it will be skip.

1/22/2013

Exception

10

Cont
The type of catch handler parameter must be match with

throw clause parameter. The syntax of catch block is catch (parameter) { catch handler code }

1/22/2013

Exception

11

Throw clause
The throw key represents that an exception has occurred. This process called throwing an exception. Following points must be noted for throw clause.
The keyword throw specifies one operand in usual cases. The operand of throw clause can be of any type. When the operand of throw clause is an object then a

temporary copy of throw operand is created and initialized. The temporary object is destroy when corresponding exception handler complete its execution.

1/22/2013

Exception

12

A C++ program which is used to declare a class for finding quotient of two numbers. In this program an error will be handled by compiler.
#include<iostream.h> class quotient { private: int a,b,c; public: quotient(int x, int y) { a=x; b=y } void findquotient() { c=a/b;
1/22/2013 Exception

} void displayquotient() { cout<<quotient =<<c; } }; void main() { quotient obj(4,0); obj.findquotient(); obj.displayquotient(); }

13

Cont
Output:
Divided-Error Exception Note :if the above program line-23 change quotient.obj(4,2); In case of replacement the output of above program will

be quotient=2

1/22/2013

Exception

14

Error handle by try, catch and throw clause


#include<iostream.h> class quotient { private: int a,b,c; quotient(int x, int y) { a=x; b=y; }
1/22/2013 Exception

void findquotient() { if(b==0) throw(value of b not zero); else c=a/b; } void displayquotient() {

15

Cont
cout<<quotient =<<c; } }; void main() { quotient obj(4,0); try { obj.findquotient(); obj.displayquotient(); } catch(char ch[]) { cout<<ch; } } Output: value of b not zero

1/22/2013

Exception

16

In this program an error handled by try, catch and throw clause and use consecutives try and catch blocks.
#include<iostream.h> class calculate { private: int a,b,c,d; public: calculate(int x,int y) a=x; b=y; } void findquotient() { if(b==0)

throw(value of b not zero); else c=a/b; } void finddifference() { if (a<b) throw(value of a less than b); else d=a-b; } void displayquotient() { cout<<quotient =<<c; }

1/22/2013

Exception

17

Cont

void displaydifference() { cout<<difference =<<d; } }; void main() { calculate obj(-3,0); try { obj.findquotient(); Obj.finddifference(); } catch(char ch[]) { cout<,ch<<endl; }

try { obj.finddifference(); obj.displaydifference(); } catch(char ch[]) { cout<<ch; } } Output: value of b not zero value of a less than b

1/22/2013

Exception

18

Exception handling with exception classes


The class whose object passed to a catch block is known

as Exception class. Following points must b noted about an exception class:


Exception class must be declaring within another class.

The access specifier for exception class must be public.


More than one data members can be declared within an

exception class.

1/22/2013

Exception

19

Cont
Like other classes an exception class can have a number of

member functions. The access specifier for data members of an exception class would be public because their data members are accessed outside the class with reference of its object. Usually an object of exception class is created when throw clause encountered. Its not necessary that catch block always receive an object of exception class. If it receive an object as parameter then it will be define as: ClassName::ExceptionClass obj;
1/22/2013 Exception 20

In this program an error will be handled by try, catch and throw clause and an error class.
#include<iostream.h> class quotient { private: int a,b,c; public: class errorclass { }; quotient(int x,int y); { a=x; b=y; }
1/22/2013

void findquotient() { if(b==0) throw errorclass(); else c=a/b; } void displayquotient() { cout<,quotient =<<c; } }; void main() {

Exception

21

Cont
quotient(4,0); try { obj.findquotient(); obj.displatquotient(); } catch(quotient::errorclass) { cout<<value of b not zero); } } Output: value of b not zero

1/22/2013

Exception

22

In this program an error will be handled by using try , catch and throw clause, and an error class with attributes.
#include<iostream.h> #include<string.h> class quotient { private: int a,b,c; public: class errorclass { public: int a; char ch[20]; errorclass(char st[], int x) { a=x; strcpy( ch, st); } }; quotient(int x, int y) { a=x; b=y; }
Exception 23

1/22/2013

Cont
void findquotient() { if(b==0) throw errorclass(change value,b); else c=a/b; } void displayquotient() { cout<<quotient =<<c; } }; void main() { quotient obj(4,0); try { obj.findquotient(); obj.displayquotient(); } catch(quotient::errorclass obj) { cout<<obj.ch<<obj.a; } } Output: change value 0

1/22/2013

Exception

24

Multiple Exceptions
More than one exception is known as multiple

exception. In multiple exception there are two exception classes. Both classes chance that an exception is occurred.

1/22/2013

Exception

25

Example of multiple exception


#include<iostream.h> const int MAX=3; class stack { private: int st[MAX]; int top; public: class full(); class empty(); stack() { top=-1; }
1/22/2013 Exception

void push(int var) { if(top>=MAX-1) throw full(); st[++top]=var; } int pop() { if(top<0) throw empty(); return st[top--]; } };

26

Cont
int main() { stack s1; try { s1.push(11); s1.push(22); s1.push(33); s1.push(44); //stack full cout<<1 :<<s1.pop()<<endl; cout<<2 :<<s1.pop()<<endl; cout<<3 :<<s1.pop()<<endl; cout<<4 :<<s1.pop()<<endl; //stack empty } catch(stack::full) { cout<<exception :stack full<<endl; } catch(stack::empty) { cout<<exception :stack empty<<endl; return 0; }

1/22/2013

Exception

27

References
Object Oriented Programming using C++ By Shahid Hussain

Minhas. http://en.wikipedia.org/wiki/Object-oriented_programming http://en.wikipedia.org/wiki/Exception_handling http://www.webopedia.com/TERM/E/exception.html

1/22/2013

Exception

28

Vous aimerez peut-être aussi