In C++ programming, an exception is an abnormal condition that arises/occurs in a source code during run time.
Or
Exception is a run time error.
Example:
1 2 3 4 5 6 7 8 9 | #include <iostream> using namespace std; int main() { int a, b; a = 0; b = 100 / a; //Error, Arithmetic Error cout << "a & b = " << a << b; } |
Output
1 | Error, Arithmetic Error |
When we try to compile this program we will receive an error message because value of 100/a (100/0) is infinite and it can’t be possible to save and print infinite value.
Handle Exception in C++
In a C++ programming language whenever an exceptional condition is arises it can be handled by the three keywords: try, catch and throw.
The Keyword try: Any code or statement that we want to monitor for an exception must be put inside the try block.
Syntax for try block:
1 2 3 4 | try { statement 1; statement2; } |
Example:
1 2 3 4 | try { statement 1; statement2; } |
In an above statement we have written try block.
Inside the try block we have placed a code (b=100/a) in which exception may occur.
Value of “ a” can be any integer value there is no problem but if the value of variable “a” is zero then error will generate because 100/a (100/0) is infinite and it can’t be possible to save and print infinite value.
The Keyword throw: If the exception occurs within try block it is automatically thrown by system or it can be manually thrown by using “throw” keyword. Syntax for throw:
throw (excep);
when excep is throw catch statement associated with the try block is handle this exception.
The Keyword catch: When the exceptional condition is arises within the try block it is thrown by the keyword throw and catch by the catch statement associated with the try block.
Syntax for catch block:
1 2 3 4 5 6 | try { statement 1; statement2; } catch (argument) { statement3; } |
Note: If there is no exception within the try block then catch block will never execute.
Example: Write a program to show exception.
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> using namespace std; int main() { int a, b; a = 0; b = 100 / a; // error, arithmetic error cout << "\n a & b =" << a << b; } |
Output
Calling show method of Shape Shape is no defined Calling show method of Circle Showing a Circle Calling show method of Rectangle Showing a Rectangle Calling show method of Square Showing a Square Calling show method of Triangle Showing a Triangle
Example: Write a program to throw an exception when a =0 otherwise perform division.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> using namespace std; int main() { int a, b; cout << "Enter the value of a ="; cin >> a; try { if (a == 0) // if a=0 then throw exception & catch by catch ststement { throw a; } b = 100 / a; cout << "\n a & b =" << a << b; } catch (int i) { cout << "Exception occurs because value of a is =" << i; } cout << "\n Exit main"; } |
Output
1 2 3 4 5 6 7 8 9 | (if a =0 then) enter the value of a = 0 // enter by user exception occurs because value of a is = 0 exit main (Second condition if a =5 then Output) enter the value of a = 5 a & b =520 exit main |
Example: Write a program to throw an exception when a =1 otherwise perform subtraction of x and y.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <iostream> using namespace std; int main() { int x, y, a; cout << "Enter the value of a ,x & u ="; cin >> a >> x >> y; try { if (a == 1) { throw a; } cout << "x-y =" << (x - y); } catch (int i) { cout << "Exception occurs because value of a is =" << i; } cout << "Exit main"; return 0; } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ( if a=0 ) enter the value of a ,x & u = 1 5 4 exception occurs because value of a is =1 exit main enter the value of a ,x & u = 2 5 4 x-y =1 exit main |
Multiple Catch Block
In a C++ programming we can define multiple catch statement for a single try block. Syntax for multiple catch is:
1 2 3 4 5 6 7 8 9 10 | try { statement 1; statement2; } catch (argument) { statement3; } catch (argument) { statement4; } catch (argument) { statement5; } |
Example: Write a program to throw multiple exception and defined multiple catch statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> using namespace std; void num(int a) { try { if (a == 0) { throw 'x'; } else { if (a > 0) throw a; else throw 2.2; } } catch (int c) { cout << "\n positive value=" << c; } catch (char i) { cout << "\n value =" << i; } catch (double d) { cout << "\n negative value =" << d; } } int main() { num(0); num(4); num(-1); } |
Output
1 2 3 | value =x positive value=4 negative value =2.2 |
Default catch()
In a C++ exception handling it is also possible to define single or default catch block for one or more exception of different types.
In such a situation, a single catch block can catch exception thrown by multiple throw statement.
Example: Write a program to throw multiple exception and defined multiple catch statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; void num(int a) { try { if (a == 0) { throw a; } else { if (a > 0) throw 'X'; else throw 2.2; } } catch (...) { // default catch cout << "\n exception"; } } int main() { num(0); num(4); num(-1); } |
Output
1 2 3 | exception exception exception |