exception

28
06/06/22 Exception 1 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

description

computer

Transcript of exception

Page 1: exception

04/08/23 Exception 1

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

Page 2: exception

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.

04/08/23 2Exception

Page 3: exception

04/08/23 Exception 3

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.

Page 4: exception

04/08/23 Exception 4

Exception handlingException 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.

Page 5: exception

04/08/23 Exception 5

Cont…In C++ exception are controlled in two ways:

By compilerBy using a set of blocks/clause such throw, try and

catch.

Page 6: exception

04/08/23 Exception 6

Cont…The exception handling mechanism is made up of the

following elements:try blockscatch blocksthrow expressions

Page 7: exception

04/08/23 Exception 7

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.

Page 8: exception

04/08/23 Exception 8

Cont…The syntax of try block is

try

{

Statement(s);

}

Page 9: exception

04/08/23 Exception 9

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.

Page 10: exception

04/08/23 Exception 10

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.

Page 11: exception

04/08/23 Exception 11

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

}

Page 12: exception

04/08/23 Exception 12

Throw clauseThe 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.

Page 13: exception

04/08/23 Exception 13

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;

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

Page 14: exception

04/08/23 Exception 14

Cont…Output:Divided-Error ExceptionNote :if the above program line-23 changequotient.obj(4,2);In case of replacement the output of above program will

be quotient=2

Page 15: exception

04/08/23 Exception 15

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;}

void findquotient()

{

if(b==0)

throw(“value of b not zero”);

else

c=a/b;

}

void displayquotient()

{

Page 16: exception

04/08/23 Exception 16

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

Page 17: exception

04/08/23 Exception 17

#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”); elsed=a-b;} void displayquotient(){ cout<<“quotient =“<<c;}

In this program an error handled by try, catch and throw clause and use consecutives try and catch blocks.

Page 18: exception

04/08/23 Exception 18

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 zerovalue of a less than b

Cont…

Page 19: exception

04/08/23 Exception 19

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.

Page 20: exception

04/08/23 Exception 20

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;

Page 21: exception

04/08/23 Exception 21

#include<iostream.h> class quotient{ private: int a,b,c; public: class errorclass{}; quotient(int x,int y);{ a=x; b=y;}

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

In this program an error will be handled by try, catch and throw clause and an error class.

Page 22: exception

04/08/23 Exception 22

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

Output:value of b not zero

Cont…

Page 23: exception

04/08/23 Exception 23

#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;}

In this program an error will be handled by using try , catch and throw clause, and an error class with attributes.

Page 24: exception

04/08/23 Exception 24

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

Cont…

Page 25: exception

Multiple ExceptionsMore than one exception is known as multiple

exception.In multiple exception there are two exception

classes.Both classes chance that an exception is

occurred.

04/08/23 25Exception

Page 26: exception

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;}

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

04/08/23 26Exception

Page 27: exception

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;}

04/08/23 27Exception

Page 28: exception

04/08/23 Exception 28

References Object Oriented Programming using C++ By Shahid Hussain

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