T5_U5_Exception_Handling
T5_U5_Exception_Handling
· Developing an error-free program is the only objective and intention of the programmer.
· A few programming languages support exception-handling features . without this feature , the
programmers should find out errors on their own.
· The logical error remains in the program due to a poor understanding of the program
· The Syntax Error made are due to a lack of understanding of the programming langauge
· C++ provides an Exception-Handling procedure to reduce the errors that the programmer
makes.
· The exception - handling technique passes the control of a program from a location of exception
in a program to an exception-handler routine linked with the try lock .
try
Syntax :
try
{
statement-1;
statment-2;
throw
Syntax:
throw (except);
throw excep;
catch
· Similar to the try block , the catch block also contains a series of statements enclosed in curly
braced.
syntax
try
statement-1
statement-2
catch(argument)
· After execution of the catch block the statements inside the blocks are executed.
· In case no exception is caught , the catch block is ignored, and if a mismatch is found , the
program is terminated
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int x=5,y=0,c;
c=x/y;
getch();
OUTOUT
division by zero
· In the above program when user divided any number by zero means the output will throw the
error like as above , but as a user it will not understandable for the user
· A programmer can solve the error with the help of exception handling
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int x=5,y=0,c=0;
try
if(y==0)
else
c=x/y;
catch(char* msg)
cout<<msg;
getch();
· In above program if x=5 and y=0 means the if statement become true
· so try will identify the error and throw the message "Dont use zero as a divisor" with the help of
throw keyword
· catch- block will receive that upcomming error from try block with the help of string argument
varaible( msg) and display the erro to the user.