Sie sind auf Seite 1von 4

Exception Handling in C++

An exception is a runtime error occurred during execution of program. (or)

An exception may result in abnormal termination of program loss of data.

Exception handling is the process of handling errors and exceptions in such a way that they do not
harm normal execution of the program.

Errors can be broadly categorized into two types. We will discuss them one by one.

1. Compile Time Errors


2. Run Time Errors

Compile Time Errors – Errors caught during compiled time is called Compile time errors.
Compile time errors include library reference, syntax error or incorrect class import.

Run Time Errors - They are also known as exceptions. An exception caught during run time creates
serious issues.

For example, User divides a number by zero, this will compile successfully but an exception or run
time error will occur

Understanding Need of Exception Handling

Let's take a simple example to understand the usage of try, catch and throw.

Below program compiles successfully but the program fails at runtime, leading to an exception.

#include <iostream>#include<conio.h>
using namespace std;
int main()
{
int a=10,b=0,c;
c=a/b;
return 0;
}

The above program will not run, and will show runtime error on screen, because we are trying to
divide a number with 0, which is not possible.

Exception handling mechanism

Exception handling is a mechanism that allows you to take appropriate action to avoid runtime errors.

C++ provides three keywords to support exception handling.

 Try : The try block contain statements which may generate exceptions.
 Throw : When an exception occur in try block, it is thrown to the catch block using throw
keyword.
 Catch :The catch block defines the action to be taken, when an exception occur.

The general form of try-catch block in c++.


Example of simple try-throw-catch
catch
#include<iostream.h>
#include<conio.h>
void main()
{
int n1,n2,result;

cout<<"\nEnter
nEnter 1st number : ";
cin>>n1;

cout<<"\nEnter
nEnter 2nd number : ";
cin>>n2;

try
{
if(n2==0)
throw n2; //Statement 1
else
{
result = n1 / n2;
cout<<"\nThe
nThe result is : "<<result;
}
}
catch(int x)
{
cout<<"\nCan't
nCan't divide by : "<<x;
}

cout<<"\nEnd
nEnd of program.";

Output :

Enter 1st number : 45


Enter 2nd number : 0
Can't divide by : 0
End of program
Multiple Catch Statements

A single try statement can have multiple catch statements. Execution of particular catch block
depends on the type of exception thrown by the throw keyword. If throw keyword send exception of
integer type, catch block with integer parameter will get execute.

Example of multiple catch blocks

#include<iostream.h>
#include<conio.h>
void main()
{
int a=2;

try
{

if(a==1)
throw a; //throwing integer exception

else if(a==2)
throw 'A'; //throwing character exception

else if(a==3)
throw 4.5; //throwing float exception

}
catch(int a)
{
cout<<"\nInteger exception caught.";
}
catch(char ch)
{
cout<<"\nCharacter exception caught.";
}
catch(double d)
{
cout<<"\nDouble exception caught.";
}

cout<<"\nEnd of program.";

Output :

Character exception caught.


End of program.
Rethrowing Exceptions

Rethrowing exception is possible, where we have an inner and outer try-catch


try catch statements (Nested
try-catch).
catch). An exception to be thrown from inner catch block to outer catch block is called rethrowing
exception.

Syntax of rethrowing exceptions

Example of rethrowing exceptions

#include<iostream.h>
#include<conio.h>
void main()
{
int a=1;

try
{
try
{
throw a;
}
catch(int x)
{
cout<<"\nException
nException in inner try-catch
try
block.";

throw x;
}

}
catch(int n)
{
cout<<"\nException
nException in outer try
try-catch block.";
}

cout<<"\nEnd
nEnd of program.";

Output :

Exception in inner try-catch


catch block.
Exception in outer try-catch
catch block.
End of program.

Das könnte Ihnen auch gefallen