Group No.6: Sumeed Javed 1515 Muhammad Kashan 1493 Talha Asghar 1520
Group No.6: Sumeed Javed 1515 Muhammad Kashan 1493 Talha Asghar 1520
6
Sumeed Javed 1515
Muhammad kashan 1493
Talha Asghar 1520
CONTENTS:
Definition of exception
Types of exceptions
Exceptional handling definition
Exceptional handling mechanism
Diagram
Exceptional handling mechanism
Advantages/ Purpose
Exceptional handling fundamentals
Keywords
Try definition
Catch definition
Throw definition
Syntax of exceptional handling
Block Diagram
Program
PRESENTER NO.1
Sumeed Javed
Roll no.bsf1601515
BS(IT) 3rd
EXCEPTION:
Definition:
“An exception is an unexpected event that occurs
during run-time and causes normal program flow to
be disrupted.”
Definition:
“Exception is an run time anomalies or unusual
conditions that a program may encounter during
execution.”
EXAMPLE OF EXCEPTION:
Divide by zero error.
Accessing the elements of an
array beyond its range.
Invalid input.
Hard disk crash.
Opening a non-existent file.
TYPES OF EXCEPTIONS:
There are two types:
Synchronous exception:
Out of range
Over flow
Asynchronous exception: Error that are
caused by causes beyond the control of the
program
Keyboard interrupts
In C ++ only Synchronous exception can be
handled.
EXCEPTIONAL HANDLING:
Definition:
“The way of handling anomalous
situation in a program-run is known
as exception handling.”
EXCEPTIONAL HANDLING
MECHANISM:
Try
Catch
Throw
KEYWORDS:
Try: “The keyword try is used to preface a
block of which may generate exceptions.”
Throw: “When an exception is detected, it is
the thrown using a throw statement in the
try block.
Catch: “A catch block define by the keyword
‘catch’ catches the exception and handles it
appropriately.
The catch block that catches an exception
must immediately follow the try block that
throws the exceptions.
TASK OF EXCEPTION HANDLING
FUNDAMENTALS:
Hit the exception
Throw the exception
Catch the exception
Handling the exception
BLOCK DIAGRAM:
SYNTAX OF EXCEPTION
HANDLING:
try
{
… //block of statements
throw exception; //which detect and
… //throws an exception
}
catch (type arg) //catch exception
{
… //block of statements
… //that handles the
… //exception
}
PROGRAM:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b;
cout<<"Enter value of a and b"<<endl;
cin>>a>>b;
int x = a/(a-b);
cout<<"x="<<x<<endl;
getch();
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b;
cout<<"Enter value of a and b"<<endl;
cin>>a>>b;
int x = (a-b);
try{
if(x!=0){
cout<<"result:"<<a/(a-b)<<endl;
}
else
{
throw(x);
}
}
catch(int i){
cout<<"division by zero"<<endl;
}
cout<<"next statement";
getch();
return 0;
}