An exception is an abnormal condition that arises in a code sequence at run time. An exception is a run-time error.
For any program the errors could occur at compile-time or at run-time. When we compile using Java Compiler (
As shown in the above example, if we create an array of size
Similarly, when we divide a number by zero, the value will become Infinite, since Infinite can not be stored in
So, it is necessary for Java to throw the exceptions in situations like these. Below is the exception thrown by the previous program when a number is divided by zero. But a non-technical user of this program will have difficult time understanding this exception.
So, as programmers we need to handle these exceptions better and show understandable messages to the users. The exceptions handling can be done either by preventing it or processing the exception after it has occurred. The prevention of exceptions is explained in How To Handle An Exception In Java and handling of the exceptions post there occurrence is explained in Exception Handling In Java with Example Program.