No: 09
1.
Experiment
vision:
To
write a program on the concept of Exception Handling.
2.
OBJECTIVE:
Program to implement Exception Handling.
3.1
THEORY:
Exceptions
in C++ are implemented using three keywords that work in conjunction
with each other: throw, try,
and catch.
In
C++, a throw statement is
used to signal that an exception or error case has occurred.
Signaling that an exception has occurred is also commonly
called raising an
exception.
To
use a throw statement, simply use the throw keyword, followed by a
value of any data type you wish to use to signal that an error has
occurred. Typically, this value will be an error code, a description
of the problem, or a custom exception class.
3.2
PROGRAM:
1)
Following is an example which makes use of Exception Handling.
#include
<iostream>
using
namespace std;
int
main( )
{
int
a,b;
cout<<"\n
Enter value of a and b:";
cin>>a>>b;
int
x=a-b;
try
{
if(x!=0)
{
cout<<"Result(a/x)="<<a/x<<"\n";
}
else
{
throw(x);
}
}
catch(int
i)
{
cout<<"\n
Exception caught: x="<<x<<"\n";
}
return
0;
}
3.3
OUTPUT:
4.
conclusion:
Thus
a
C++ program by using exception handling has
been executed successfully.
5.
PRECAUTIONS:
1.
Avoid wrong keywords.
2.
Check whether all brackets are closed properly or not.
3.
Take desired output.
4.
In case of abnormal results, Contact
the teacher/instructor, repeat the
experiment,
and check the algorithm and program.
6.
REMARK:
Exception
handling detects the error and throws the exception.
7.
Discussion
Questions:
1)
Explain try, catch and throw block.
____________________________________________________________________________________________________________________________________________________________
2)
What are the task that try and catch block perform?
____________________________________________________________________________________________________________________________________________________________
3)
What are the two types of exceptions?
____________________________________________________________________________________________________________________________________________________________
4)
Explain the sequence of events occurred when exception occurs.
____________________________________________________________________________________________________________________________________________________________
5)
Explain multiple exception concept.
____________________________________________________________________________________________________________________________________________________________
No comments:
Post a Comment