Exception Handling in C++
What is Exception Handling in C++?
Exception handling is performed in C++ using try, catch and throw. These help in making sure that the whole program runs completely, even if some minute runtime errors that may occur due to logical, boundary and other problem in the program.
Example :
If at run time an arithmetic operation happening has operational input that is dividing by 0.
When we talk about errors there are the following two majorly –
- Run Time errors – These are called exceptions that we can handle using try, catch and throw keywords. (Ex – Dividing by 0)
- Compile time errors – These are the error that stop the program at compile time itself. (Example – Using string without including string library in the program headers)
For Exceptions there are again two different types which are –
- Synchronous – Caused by how the program is written logically.
- Asynchronous – Which happens because of reasons which are beyond programmers control. Example Disk Failure
How this works
Try
– This is the part of code, where a programmer will assume an error to occur. The programmer will surround the code inside a try block.Catch
– This is the part of the code, where the programmer is catching and identifying which exception has occurred and is trying to handle it.Throw
– While the system may notify which type of exception has occurred. Sometimes, logically you may want to notify an error that is specific to your code logic. These is where you throw exception.
Let’s look at the basic structure of try and catch, which is essentially you trying a code that may throw an error and catch is capturing the type of error that has occurred.
try { // code that is tried } catch( NameOfException e1 ) { // catch block, } catch(NameOfException e2 ) { // catch block } catch(NameOfException eN ) { // catch block }
Example –
Let’s write a code for division by 0
#include <iostream> using namespace std; double divide(int value1, int value2) { if( value2 == 0 ) { throw "Exception happens division by 0"; } return (value1/value2); } int main () { int a = 200; int b = 0; double temp; try { temp = divide(a, b); cout << temp << endl; } catch (const char* message) { cerr << message << endl; } return 0; }
Output –
Exception happens division by 0
Program Flow
It is very important to understand the flow of the program –
- Try is executed
- If Exception occurs or throw statement occurs
- Immediate jump to catch statement, which then is executed.
- If no exception or thor statement
- Then catch statement is never executed.
The following program will help you understand the flow –
#include <iostream>
using namespace std;
int main()
{
int temp = 0;
cout << "(Here 1) Currently before the try statement" <<endl;
try
{
cout << "(Here 2)Currently inside the try statement" << endl;
if (temp == 0)
{
throw temp;
cout << "(Here 0) Since, this is after throw statement, flow jumps to catch and this never executed" << endl;
}
}
catch (int x ) {
cout << "(Here 3) Flow transferred to catch block" <<endl;
}
cout << "(Here 4) Try catch and throw blocks executed back to int main block";
return 0;
}
Output -
(Here 1) Currently before the try statement
(Here 2)Currently inside the try statement
(Here 3) Flow transferred to catch block
(Here 4) Try catch and throw blocks executed back to int main block
Catch All
There is a catch all exception which is catch(…) keyword in block. Let us look how it works.
#include <iostream>
using namespace std;
int main()
{
try
{
throw "PrepInsta";
}
catch (int x)
{
cout << "Integer was caught " << x;
}
catch (...)
{
cout << "Default Exception was called here\n" <<endl;
}
return 0;
}
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment