OODP UNIT 04
07 December 2022 07:32 AM
EXCEPTION HANDLING:
• Exception: “An abnormal condition that arises in a code sequence at run time”.
• An exception is a run-time error.
• Exception handling allows us to manage run-time errors in an orderly fashion.
• An exception is a problem that arises during the execution of a program.
• A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by
zero.
• Exceptions provide a way to transfer control from one part of a program to another.
• C++ exception handling is built upon three keywords: try, catch, and throw.
Using exception handling, our program can automatically invoke an error-handling routine when an error occurs.
C++ exception handling is built upon three keywords: try, catch, and throw
try identifies a code block where exception can occur
throw causes an exception to be raised (thrown)
catch identifies a code block where the exception will be handled (caught)
General Syntax: TRY
try {
… throw an exception …
}
catch the exception
General Syntax: THROW
throw exception;
General Syntax: CATCH
catch (type argument) {
// catch block
}
3) Exception handling for Divide by zero Exception:
=>Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
float d;
clrscr();
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
cout<<"Enter the value of c:";
cin>>c;
try
{
if((a-b)!=0)
{
d=c/(a-b);
cout<<"Result is:"<<d;
}
else
{
throw(a-b);
}
}
catch(int i)
07 December 2022 07:32 AM
EXCEPTION HANDLING:
• Exception: “An abnormal condition that arises in a code sequence at run time”.
• An exception is a run-time error.
• Exception handling allows us to manage run-time errors in an orderly fashion.
• An exception is a problem that arises during the execution of a program.
• A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by
zero.
• Exceptions provide a way to transfer control from one part of a program to another.
• C++ exception handling is built upon three keywords: try, catch, and throw.
Using exception handling, our program can automatically invoke an error-handling routine when an error occurs.
C++ exception handling is built upon three keywords: try, catch, and throw
try identifies a code block where exception can occur
throw causes an exception to be raised (thrown)
catch identifies a code block where the exception will be handled (caught)
General Syntax: TRY
try {
… throw an exception …
}
catch the exception
General Syntax: THROW
throw exception;
General Syntax: CATCH
catch (type argument) {
// catch block
}
3) Exception handling for Divide by zero Exception:
=>Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
float d;
clrscr();
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
cout<<"Enter the value of c:";
cin>>c;
try
{
if((a-b)!=0)
{
d=c/(a-b);
cout<<"Result is:"<<d;
}
else
{
throw(a-b);
}
}
catch(int i)