Introduction:
Exceptions are the events that prevent the normal flow of a program. It interrupts the program by terminating it and gives the computer-generated error message. These exceptions can be handled easily in java.
Exceptions can occur because of many reasons like dividing a number by 0 or accessing an out-of-bound index of an array.
Also read, Duck Number in Java and Hashcode Method in Java
Types of Exceptions and Error:-
-
Checked Exceptions: These are the exceptions that are notified to the user by the compiler at the time of the runtime. For example, if you are accessing a file using the FileReader class and the file passed to the object does not exist then it will give a FileNotFoundException exception.
-
Unchecked Exceptions: These are the exceptions that occur only when the program runs. This is the reason why they are also called Runtime exceptions. They are ignored at the compile time. For example, if we are accessing the out-of-bound index of an array then we would get an ArrayIndexOutOfBoundsException.
- Errors: These are the unexpected operation performed by a programmer that results in the abnormal working of the program. These errors can be identified once the code is compiled or run. Some of these error prevents the code from compiling and running. For example, if we are dividing a number by zero then we would get a DivisionByZero error. Also, if the stack overflow occurs then the error will arise.