Problems that occur during program execution are called exceptions. In C++ exceptions are responses to anomalies that arise while the program is running, such as an attempt to divide by zero.
C++ exception handling is built upon three keywords: try, catch, and throw.
throw is used to throw an exception when a problem shows up.
For example:
A try block identifies a block of code that will activate specific exceptions. It's followed by one or more catch blocks. The catch keyword represents a block of code that executes when a particular exception is thrown. Code that could generate an exception is surrounded with the try/catch block. What type of exception is required to catch by the exception declaration can be specified, by the definition that appears in parentheses following the keyword catch.
Exception handling is particularly useful while dealing with user input. For example, for a program that requests user input of two numbers, and then outputs their division, be sure to handle the division by zero, in case the user enters 0 as the second number.
int main() {
int num1;
cout <<"Enter the first number:";
cin >> num1;
int num2;
cout <<"Enter the second number:";
cin >> num2;
cout <<"Result:"<<num1 / num2;
}
This program works perfectly fine if the user enters any number besides zero. In case of zero, the program crashes. So the input needs to be handled. So, in the event that the second number is zero, the need for throwing an exception arises.
int main() {
int num1;
cout <<"Enter the first number:";
cin >> num1;
int num2;
cout <<"Enter the second number:";
cin >> num2;
if(num2 == 0) {
throw 0;
}
cout <<"Result:"<<num1 / num2;
}
This code throws an exception with the code 0 of type integer. Now, to handle the thrown exception using a try/catch block :
int main() {
try {
int num1;
cout <<"Enter the first number:";
cin >> num1;
int num2;
cout <<"Enter the second number:";
cin >> num2;
if(num2 == 0) {
throw 0;
}
cout <<"Result:"<<num1 / num2;
}
catch(int x) {
cout <<"Division by zero!";
}
}
This results in the output of "Division by zero!" as an alternative to a program crash, when 0 is entered as the second number. In this case, only the exceptions of type integer are caught. It's possible to specify that the catch block handles any type of exception thrown in a try block. To accomplish this, add an ellipsis (...) between the parentheses of catch :
try {
// code
} catch(...) {
// code to handle exceptions
}
C++ exception handling is built upon three keywords: try, catch, and throw.
throw is used to throw an exception when a problem shows up.
For example:
int motherAge = 29;
int sonAge = 36;
if (sonAge > motherAge) {
throw "Wrong age values";
}
int sonAge = 36;
if (sonAge > motherAge) {
throw "Wrong age values";
}
The code looks at sonAge and motherAge, and throws an exception if sonAge is found to be the greater of the two.
In the throw statement, the operand determines a type for the exception. This can be any expression. The type of the expression's result will determine the type of the exception thrown.
A try block identifies a block of code that will activate specific exceptions. It's followed by one or more catch blocks. The catch keyword represents a block of code that executes when a particular exception is thrown. Code that could generate an exception is surrounded with the try/catch block. What type of exception is required to catch by the exception declaration can be specified, by the definition that appears in parentheses following the keyword catch.
try {
int motherAge = 29;
int sonAge = 36;
if (sonAge > motherAge) {
throw 99;
}
}
catch (int x) {
cout<<"Wrong age values - Error "<<x;
}
//Outputs "Wrong age values - Error 99"
int motherAge = 29;
int sonAge = 36;
if (sonAge > motherAge) {
throw 99;
}
}
catch (int x) {
cout<<"Wrong age values - Error "<<x;
}
//Outputs "Wrong age values - Error 99"
The try block throws the exception, and the catch block then handles it. The error code 99, which is an integer, appears in the throw statement, so it results in an exception of type int.
Multiple catch statements may be listed to handle various exceptions in case multiple exceptions are thrown by the try block.
Exception handling is particularly useful while dealing with user input. For example, for a program that requests user input of two numbers, and then outputs their division, be sure to handle the division by zero, in case the user enters 0 as the second number.
int main() {
int num1;
cout <<"Enter the first number:";
cin >> num1;
int num2;
cout <<"Enter the second number:";
cin >> num2;
cout <<"Result:"<<num1 / num2;
}
This program works perfectly fine if the user enters any number besides zero. In case of zero, the program crashes. So the input needs to be handled. So, in the event that the second number is zero, the need for throwing an exception arises.
int main() {
int num1;
cout <<"Enter the first number:";
cin >> num1;
int num2;
cout <<"Enter the second number:";
cin >> num2;
if(num2 == 0) {
throw 0;
}
cout <<"Result:"<<num1 / num2;
}
This code throws an exception with the code 0 of type integer. Now, to handle the thrown exception using a try/catch block :
int main() {
try {
int num1;
cout <<"Enter the first number:";
cin >> num1;
int num2;
cout <<"Enter the second number:";
cin >> num2;
if(num2 == 0) {
throw 0;
}
cout <<"Result:"<<num1 / num2;
}
catch(int x) {
cout <<"Division by zero!";
}
}
This results in the output of "Division by zero!" as an alternative to a program crash, when 0 is entered as the second number. In this case, only the exceptions of type integer are caught. It's possible to specify that the catch block handles any type of exception thrown in a try block. To accomplish this, add an ellipsis (...) between the parentheses of catch :
try {
// code
} catch(...) {
// code to handle exceptions
}
Comments
Post a Comment