Exception Handling in C++ with Examples

Exception Handling in C++ with Examples

1. Exception is any abnormal behaviour of the program or it shows runtime error.

2. Exception handling is a process which helps in handling runtime errors inside any programs.

2. Exceptions are off beat situation in your program where your program should be ready to handle it with appropriate response.


Exception Handling

C++ provides a built-in error handling mechanism that is called exception handling used for handle exception.

You can more easily handle, manage and respond to run time errors using exception handling.


try, catch and throw in c++

1. Program statements that you want to monitor for exceptions are contained in a "try" block.

2. If any exception occurs within the "try" block, it is thrown (using "throw").

3. The exception is then caught using "catch" and processed.


Syntax:

try
{
//... try block
}

catch(type1 argument)
{
//... catch block
}

catch(type2 argument)
{
//... catch block
}

catch(typeN argument)
{
//... catch block
}


Try

1. Try block contains those statements that you want to check for exceptions are thrown or not.

2. The exceptions, if found inside the try block, are thrown.


Catch

1. When an exception is caught, argument present in catch block next to try block will receive its value.

2. If you don't need access to the exception itself specify only type in the clause, argument is optional in the catch.

3. Any type of data can be caught including classes that you have created in the program.


Throw

1. The general form of the throw statement is: throw exception;

2. Throw must be executed properly either within the try block or from any other function that code within the block calls.


Let us consider a simple C++ program
#include<iostream>
using namespace std;
int main()
{
cout << "Welcome to Learning Mania" << endl;

throw 12;

cout << "In try" << endl;

cout << "The last line of code" << endl;

return 0;
}

The above written program will first execute and run then it will stop after a few interval of time and then it will get terminated.

It will show "terminate called after throwing an instance of 'int' ". Here, a function named terminate is automatically called by the C++ compiler, which will further call abort function. The abort function would end the program.


Points To Remember:-

1. Whenever an exception thrown by try is not being handled by catch, a function named terminate is automatically called by the C++ compiler, which will further call abort function. The abort function would end the program.

#include<iostream>
using namespace std;
int main()
{
cout << "Welcome to Learning Mania" << endl;

try
{
throw 11;
cout << "In TRY block" << endl;
}

catch (double E)
{
cout << "Exception No. is: " << E << endl;
}

cout << "In LAST Line" << endl;
}

The above written program will first execute and run then it will stop after a few interval of time and then it will get terminated.

It will show "terminate called after throwing an instance of 'int' ". Here, a function named terminate is automatically called by the C++ compiler, which will further call abort function. The abort function would end the program.

In the above code, catch is not handling the throw statement because throw is int and catch is handling double datatype. So, the catch is not handling throw statement and hence there is runtime error.

2. If any catch is executed for given throw statement, then the forward written catches will not executed for that throw, In fact, the compiler will not even see the forward written catch block, it just ignores all catch for that throw statement.

#include<iostream>
using namespace std;
int main()
{
cout << "Welcome to Learning Mania" << endl;

try
{
throw 11;
cout << "In TRY block" << endl;
}

catch (double E)
{
cout << "Exception No. is: " << E << endl;
}

catch (int I)
{
cout << "Exception No. is: " << I << endl;
}

cout << "In LAST Line" << endl;
}

Output:

Welcome to Learning Mania
Exception No. is: 11
In LAST Line


3. If any throw statement get executed once inside try block, the statements written after that throw will never execute inside try block. for example:

#include<iostream>
using namespace std;
int main()
{
try
{
cout << "In try 1." << endl;

throw 12;

cout << "In try 2." << endl;
}

catch(int)
{
cout << "Exception thrown inside try block is caught.";
}

return 0;
}

Output:

In try 1.
Exception thrown inside try block is caught.


4. If you write try in the code then you will have to write catch and if catch is written, you will have to write try but remember that catch will always come just after try block. for example:

#include<iostream>
using namespace std;
int main()
{
cout << "Welcome to Learning Mania" << endl;

try
{
throw 11;
cout << "In TRY block" << endl;
}

catch (double E)
{
cout << "Exception No. is: " << E << endl;
}

catch (int I)
{
cout << "Exception No. is: " << I << endl;
}

cout << "In LAST Line" << endl;
}


5. You must write data (or exception details) after writing throw inside the code. for example:

#include<iostream>
using namespace std;
int main()
{
int num1 = 11;

cout << "Welcome to Learning Mania" << endl;

try
{

if (num1 == 11)
{
throw;
}

cout << "In TRY block" << endl;
}

catch (int)
{
cout << "Exception";
}

return 0;
}

Here, catch will not handle the throw statement because there is no exception details written after throw

It will show "terminate called without an active exception". Here, a function named terminate is automatically called by the C++ compiler, which will further call abort function. The abort function would end the program.


6. Throw can also be implemented inside any function and calling that function inside the try block. for example:

#include<iostream>
using namespace std;

void Function1()
{
throw 101;
}

int main()
{
try
{
Function1();
}

catch (int Int1)
{
cout << "Exception No. is: " << Int1 << endl;
}

cout << "In LAST Line";
return 0;
}

Output:

Exception No. is: 101
In LAST Line


7. There is a special catch block called "catch all" or catch (...) It is used to catch all types of exceptions. for example:

#include<iostream>
using namespace std;

int main()
{
int num = 11;

try
{
if (num == 8)
{
throw "Learning";
}

if (num == 10)
{
throw "Mania";
}

if (num == 11)
{
throw 11.0;
}
}

catch (...)
{
cout << "Catch All Executed";
}

return 0;
}



<< Previous                                                                                      Next >>
Previous
Next Post »