THE COMPUTER PROGRAM IS RESPONSIBLE FOR CATCHING AN EXCEPTION
WHAT IS AN EXCEPTION?
An Exception is a glitch that interrupts the normal flow of a program and therefore results in program termination without completing the execution process. Exception handling using keywords such as try, throw and catch is very important so the program can continue to execute instead of terminating or resulting in compilation error. There are two types of errors generally known, these are syntax error or a runtime error which simply creates an exception. When this occur, the program prints a traceback message pointing to where the program error is and so therefore, the programmer’s responsibility to read, understand and trace where the program is point and then resolve the issue so the program is able to handle properly.
HOW A COMPUTER PROGRAM HANDLES EXCEPTION
The program throws an exception when an unwanted event occurs by displaying a keyword; by this, the program catches the exception at the point where it occurred and with an exception handler such as the ‘catch keyword’ to handle the exception. If an exception is not handled an error may occur and the program may, therefore, be terminated.
Exceptions are classified into two types known as the checked and unchecked exceptions.
In checked exception, the compiler checks to verify if the program has handled the exceptions during the compilation process, if it has been handled, the program executes; otherwise, it gets terminated due to compilation error. It usually referred to as Mandatory Exception Handling, where the compiler checks whether exceptions are handled by the program.
Unchecked exceptions which are also referred to as runtime exception, are not checked by the compiler to verify if the program has handled exceptions in the code or not and therefore executes. An unchecked exception (RuntimeException) usually signifies a bug in the program, which the programmer is responsible for fixing .
SUMMARY OF HANDLING EXCEPTION
A well and accurately compiled program will check for exceptions and handle them properly, therefore, there is a need for the programmer to check and catch the exception by handling or declaring statements that enable the code to pass during the compilation and the runtime processes; also, if done properly, a program will properly handle the exception, detected unforeseen errors can be managed without crashing or terminating the program.