2. Exceptions provide a way to react to exceptional
circumstances (like runtime errors) in programs by
transferring control to special functions called handlers.
An exception is an event, which occurs during the
execution of a program, that disrupts the normal flow of the
program's instructions.
Exception handling provides a standard mechanism for
coding responses to run-time errors or exceptions.
3. Some common types :
Divide by zero errors.
Accessing the elements of an array beyond its range.
Invalid input.
Hard disk crash.
Opening a non-existing file.
Heap memory exhaustion.
4. C++ exception handling is built upon three keywords: try, catch,
finally and throw.
1. try: A try block identifies a block of code for which particular exceptions
will be activated. It's followed by one or more catch blocks.
2. catch: A program catches an exception with an exception handler at the
place in a program where you want to handle the problem. The
catch keyword indicates the catching of an exception.
3. finally: The finally block allows you to run any cleanup-type statements
that you want to execute, no matter what happens in the protected code.
4. throw: A program throws an exception when a problem shows up. This
is done using a throw keyword.
6. The throw statement requires a single argument: a throwable object.
Throwable objects are instances of any subclass of the Throwable class. Here's
an example of a throw statement.
Example:
try {
< Connecting to server >
if (< Not Connecting to Server >) {
throw(“Server not responding”);
}
}
7. try {
< Connecting to server >
if (< Not connected to server >) {
throw(“Server not responding”);
}
} catch (string ErrorMessage) {
cout << ErrorMessage;
}
8. try {
< Connecting to server >
if (< Not connected to server >) {
throw(“Server not responding”);
}
} catch (string ErrorMessage) {
cout << ErrorMessage;
} finally {
< Connecting to server >
}
9. Exception handling separates error-handling code from normal
code.
It clarifies the code and enhances readability.
It makes for clear, robust and fault-tolerant programs.
10. Insaan Ka Apnay Dushman Sey Inteqaam Ka
Sub Sey Acha Tariqa Ye He Key Wo Apni
Khoobiyo’n Mein Izafa Karey “Hazrat Ali
(A.S)”