Exception Handling in c++
Exception Handling in c++
Walia Fatima
Exception Handling
• An exception indicates an unusual situation that occurs during a program’s execution.
The process of handling these exceptions is called exception handling.
• Using the exception handling mechanism, the control from one part of the program
where the exception occurred can be transferred to another part of the code.
• When an error occurs, C++ will normally stop and generate an error message. The
technical term for this is: C++ will throw an exception (throw an error).
C++ try and catch
Exception handling in C++ consist of three keywords: try, throw and catch:
• The try statement allows you to define a block of code to be tested for errors while it is
being executed.
• The throw keyword throws an exception when a problem is detected, which lets us create
a custom error.
• The catch statement allows you to define a block of code to be executed, if an error occurs
in the try block.
• The try and catch keywords come in pairs:
Syntax for Exception Handling in C++
try {
catch (exception) {
// code to handle exception
}
Example (Quotient.cpp)
#include <iostream>
using namespace std;
int main(){
cout << "Enter two integers: ";
int number1, number2;
cin >> number1 >> number2;
cout << number1 << " / " << number2 << " is "<< (number1 / number2) << endl;
return 0;
}
If you enter 0 for the second number, a runtime error occurs, because you cannot divide an integer
by 0.
Example(QuotientwithIf.cpp)
#include <iostream> else
using namespace std; {
int main() cout << "Divisor cannot be zero" << endl;
{ }
cout << "Enter two integers: ";
int number1, number2; return 0;
cin >> number1 >> number2; }
if (number2 != 0)
{
cout << number1 << " / " << number2 << " Output:
is " << (number1 / number2) << endl; Enter two integers: 5 0
} Divisor cannot be zero
Example
(QuotientwithException.cpp)
#include <iostream> catch (int ex)
using namespace std; {
int main() cout << "Exception: an integer " << ex <<"
{ cannot be divided by zero" << endl;
cout << "Enter two integers: "; }
int number1, number2; cout << "Execution continues ..." << endl;
cin >> number1 >> number2; return 0;
try }
{
if (number2 == 0) Output
throw number1;
cout << number1 << " / " << number2 << " is " Enter two integers: 5 3
<< (number1 / number2) << endl; 5 / 3 is 1
} Execution continues ...
Enter two integers: 5 0
Exception: an integer 5 cannot be divided
by zero
Execution continues . . .
Example(QuotientWithFunction.cpp)
#include <iostream> catch (int ex)
using namespace std; {
int quotient(int number1, int number2) cout << "Exception from function: an
{ integer " << ex << " cannot be divided by
if (number2 == 0) zero" << endl;
throw number1; }
return number1 / number2; cout << "Execution continues ..." << endl;
}
int main() return 0;
{ }
cout << "Enter two integers: ";
int number1, number2; Output:
cin >> number1 >> number2; Enter two integers: 5 3
try 5 / 3 is 1
{ Execution continues ...
int result = quotient(number1, Enter two integers: 5 0
number2); Exception from function: an integer 5
cout << number1 << " / " << cannot be divided by zero
number2 << " is "<< result << endl; Execution continues ...
}
Handle Any Type of Exceptions (...)
• If you do not know the throw type used in the try block, you can use the "three dots"
syntax (...) inside the catch block, which will handle any type of exception:
try {
int age = 15;
if (age >= 18) {
cout << "Access granted - you are old enough.";
} else {
throw 505;
}
}
catch (...) {
cout << "Access denied - You must be at least 18 years old.\n";
}
C++ Multiple catch Statements
try {
// code
}
catch (exception1) {
// code
}
catch (exception2) {
// code
}
catch (...) {
C++ Multiple catch Statements (Example 1)
#include<iostream> catch(char a) {
using namespace std;
void func(int a) { cout << "Character Exception!\n";
try { }
catch(...)
if(a==0) throw 23; {
cout<<"caught exception";
if(a==1) throw 23.33; }
}
if(a=='d') throw 's';
int main()
} {
catch (int a) func(0);
{ func(1);
cout<<"Integer exception\n"; func('d');
}
return 0;
}
C++ Multiple catch Statements
(Example
#include <iostream>
2) if (denominator == 0)
using namespace std; throw 0;