In C++, an exception is an unusual exceptional event describing an error, which occurs only during the runtime of a program.
When an exception occurs, it is said to be thrown by the C++ compiler.
If this thrown exception is not handled in the program,
it stops the normal execution of the program and it ends abruptly.
That's why we should handle an exception when it is thrown by the program. This process of handing the exception is called Exception Handling.
Some factors due to which an exception may arise during the execution of a program -:
1) When an integer number is divided by zero, it raises an exception.
2) When we try to access an array with an out-of-range value, an exception is thrown.
Types of Exception -:
Java Exceptions are divided into two categories -
1) Synchronous Exceptions These errors that are raised due to the problem in programming logic which results in issues as division-by-zero or trying to access
an out-of-range index in an array.
2) Asynchronous Exceptions These errors are raised by the events beyond the control of a program, such error or exceptions such as keyboard interrupts are not meant to be handled through exception handling.
What happens when an exception is thrown?
Exceptions are thrown at the runtime of a program. If an exception thrown during the runtime of a program is not handled within the program,
the program terminates right away without continuing any further.
Let us understand this by an example here.
Looking at the diagram above, it is easy to understand that if an exception is thrown when statement2 is executed, the program ends abruptly without executing any statement after it.
Hence, statement3 and statement4 will not be executed as a result of an exception at statement2.
In the next few articles, we will discuss three important keywords used to handle exceptions - try, catch and throw.