The term “exception” is used to represent a runtime error, and exception handling is about anticipating exceptions, identifying them, and taking corrective steps when they occur to avoid abnormal termination of the program.
The two most common errors are:
Syntactical errors occur if programs do not conform to the grammatical rules of the language being used. These errors are caught during the compilation itself.
We can correct syntactical errors and recompile programs until the compiler is satisfied with the syntactical correctness of the programs and produces the equivalent object code.
Logical errors occur if the solution procedure for the given problem is wrong itself. In this case, the output produced by the program would be incorrect.
Correcting the solution procedure itself through a better understanding of the problem will eliminates these errors.
Another type of error is Runtime errors. As the name itself indicates, they happen during the course of a program’s execution. Runtime errors occur due to internal factors and external factors.
Examples of runtime errors due to internal factors include “Division by zero," “Array index out of bounds," etc.
Examples of runtime errors due to external factors include “Running out of memory," “Printer out of paper," etc.
#include <iostream>using namespace std;int main(){try {throw 10;}catch (char *excp) {cout << "Caught " << excp;}catch (...) {cout << "Default Exception\n";}return 0;}
try
, catch
, and throw
.The try
keyword is followed by a block of statements enclosed within braces that may raise exceptions.
The block is called a try
block.
The general form of try
block is:
try
{
…..
…. //statements for detecting exception
throw exception;
}
If an exception is generated within the try block, the throw keyword informs it to the error handler routine. If an exception is informed to the error handler routine by a throw statement, the exception is said to have occurred.
The syntax for using the throw
block is:
throw exception;
The catch
keyword is enclosed in a pair of parentheses and followed by a set of statements enclosed with braces. The block is called the catch
block.
The general form of the catch block is:
catch(type argument)
{
…….//statement that handle the exception thrown by the try block
}
The catch block is used to receive (catch) the exception thrown by the throw statement of the try block and then handle it appropriately.
The general form of these blocks is:
try
{
…..
….. //statements for detecting exception
throw exception;
}
catch(type argument)
{
….//statements that handle the exception thrown by the try block
}
A catch block that catches an exception must follow the try block that throws the exception.
An exception is an object used to transmit the error situation to the catch block so that it can take corrective steps.
The object can be a built-in type value like int
, float
, char
, etc., or it can be a user-defined type value like an object of a class.
If the try block does not detect any exception, then the matching catch block is skipped during execution. If the try block detects and throws an exception, the control is transferred from the try block and reaches the catch statement of the matching catch block, i.e., the catch block of the argument type that matches with that of the exception thrown by the try block.
The catch block receives the exception and handles it. After the execution of the catch block, the control reaches the first statement after the closing brace of the catch block.
Note: the control is transferred to the try block that threw the exception.
If the try block detects an exception and throws it and there is no matching catch block, the program is terminated. If the try block does not detect an exception, then the matching catch block is skipped during execution.