Exception Handling
Exception Handling
Why?
Algorithm:
Step1: Get the input for number of times the operation has to be repeated , say n
Step2: Repeat until n less than zero
Step2: Get the input for a, b and c
Step3: Check for b and c unequality
Step4: if b and c are unequal , perform the operation a/(b-c),
Step5: else throw exception saying divide by zero and continue for next iteration
Exception Handling
Exception: An unwanted unexpected event that disturbs normal
flow of the program is called exception.
Example:
SleepingException
TyrePunchuredException
FileNotFoundException ...etc
It is highly recommended to handle exceptions. The main
objective of exception handling is graceful (normal) termination of
the program.
This process involves separate error handling code that performs the
following tasks
Identify the problem (Hit the exception)
19
Exception Handling Model
try block
Exception object
catch block
20
The keyword try is used to preface a block of statements
surrounded by braces which may be generating
exceptions; this block of statements is known as try
block
21
Exception Handling: Format
try
{
……
throw exception; // block of statements
// which detects and
// throws an exception
}
22
Throw point
catch block
26
Most often, exceptions are thrown by functions that are invoked
from within the try block
The point at which the throw block is executed is called the throw
point
27
Invoking Function that Generates Exception
inside the try block
void divide (int x, int y, intz){
inside the function
cout << " inside the function \n “;
Result = 3
if (( x-y) !=0)
Caught the exception
{ int R = z/(x–y) ;
cout<< " Result = “ << R << “\n”; }
else // there is a problem
{throw (x-y);// throw point }
}
void main() {
try {cout<< ” inside the try block \n”;
divide(20,10,30); // Invoke divide
divide(30,30,60); // Invoke divide
}
catch (int i) {
cout<< “ caught the exception \n “;
}
} 28
Catching Mechanism
29
Multiple Catch Statements
It is possible that a program segment has more than one condition to throw an
exception; such cases, you can associate more than one catch statements with a try as
shown in the following code
try
{ // try block }
catch (type1 arg)
{ // catch block1 }
catch (type2 arg)
{ // catch block2 }
……….
……….
catch (typeN arg)
{ // catch blockN }
30
catch(char c)
{ cout<<"caught a charcter"<<endl; }
catch(int m)
#include<iostream> {cout<<"caught a integer"<<endl; }
using namespace std; catch(double d)
void test(int x) { cout<<"caught a float"<<endl;}
{ cout<<"end of try catch"<<endl;
try }
{ main()
if(x==1) {
throw x; cout<<"testing multpile catches"<<endl;
else cout<<"x==1"<<endl;
if(x==0) test(1);
throw 'x'; cout<<"x==0"<<endl;
else test(0);
if(x==-1) cout<<"x==-1"<<endl;
throw 1.0; test(-1);
cout<<"end of try"<<endl; } cout<<"x==2"<<endl;
test(2);
}
In such situations you can force a catch statement to catch all exceptions
instead of a certain type alone
For example:
catch ( …)
{
// statements for processing all exceptions
}
33
void test ( int x) {
try
{ if(x==0) throw x; //int
if(x==-1) throw „x‟; //char
if(x==1) throw 1.0; //float
}
catch(…) // catch all
{
cout<<” caught an exception”;
}
}
void main() {
cout << “ Test for common catch “;
test(-1); test(0); test(1);
}
34
Independent Reference
Scenario: Assume you are Mr. Edwin and also you have a pet name called Buddy in your
home. When Buddy is ill, shall Edwin can take a pill to get cured. This is practically
possible, even though the names are different, body is single. A body can be
referred by different name.
35
Reference Variable
A reference is an alias; when you create a reference, you initialize it with the name of
another object, the target
From that moment on, the reference acts as an alternative name for the target, and
anything you do to the reference is really done to the target
38
One or more reference can be binded to the same variable, all reference variable refers
the same target
int main() {
int a=10;
int &a1ref=a;
int &a2ref=a;
int &a3ref=a;
int &a4ref=a;
int &a5ref=a;
++a1re; ++a2ref; ++a3ref; ++a4ref; ++a5ref;
cout<<" a is "<<a; }
a is 15
39
WIN 2015-16, CSE1002 Problem Solving with Object Oriented Programming
WIN 2015-16, CSE1002 Problem Solving with Object Oriented Programming
Passing Function Argument by Reference
Problem:
Assume that in a banking operation scenario, you are making a fund transfer operation
from one account to the other of the same bank and branch. Here after transaction, the
balance amount field in both the objects should be updated, i.e. debit should happen in
source account and credit should happen in destination account which in turn update the
balance amount field of both the objects.
To enable these type of operation successfully, both the objects involved in the above
operation should be send by reference, failing which inconsistent data may prevail in the
operating objects
Passing Function Argument by Reference
Functions has two limitations: Arguments are passed by value, and the return statement
can return only one value
Passing values to a function by reference can overcome both of these limitations.
In C++, passing by reference is accomplished in two ways: using pointers and using
references.
The syntax is different, but the net effect is the same.
Rather than a copy being created within the scope of the function, the actual original
object is passed into the function.
Passing Function Argument by Reference is also called as returning multiple arguments
Demonstrates passing by value
#include<stdio.h>
printf("\n in main after transfer");
struct bank
printf("\n b1.balanceis %f",b1.balance);
{int accno;
printf("\n b2.balanceis %f",b2.balance);
float balance;
}
};
void fundtransfer(struct bank b1,struct bank
void fundtransfer(struct bank,struct bank,float); b2,float amount)
int main() { {
float amount; printf("\n in function before transfer");
struct bank b1={101,1500}; printf("\n b1.balanceis %f",b1.balance);
struct bank b2={102,3000}; printf("\n b2.balanceis %f",b2.balance);
printf("enter the amount to be credited"); b1.balance=b1.balance+amount;
scanf("%f",&amount); b2.balance=b2.balance-amount;
printf("\n in main before transfer"); printf("\n in function after transfer");
printf("\n b1.balance is %f",b1.balance); printf("\n b1.balanceis %f",b1.balance);
printf("\n b2.balance is %f",b2.balance); printf("\n b2.balanceis %f",b2.balance);
fundtransfer(b1,b2,amount); }
Output:
enter the amount to be credited 1000