Top 5 Exceptions in Java with Examples
An unwanted unexpected event that disturbs the normal execution of the program after getting compiled while running is called an exception. In order to deal with such abrupt execution of the program, exception handling is the normal termination of the program.
Illustration: Considering a real-life example
Suppose books requirement is from Delhi library at runtime if Delhi library is not available to provide books due to some transport issues. Then Delhi library suggests taking books from Indore library to continue the rest of our work. This way of defining alternative is nothing but exception handling.
Exceptions are divided as follows:
- Checked exceptions: These are the exceptions that can be detected by the compiler at the time of execution of the program and warning messages are displayed.
- Unchecked Exceptions: These are the exceptions that are missed out from the compiler check giving birth to abrupt flow.No warning messages are displayed as these exceptions are not detected by the compiler.
Now the above two types of exceptions are categorized as follows:
- Generic programming exceptions: Generic programmatic exception are referred to those exceptions which are raised explicitly by the programmer (or) by the API developer are called programmatic exceptions. Example include IllegalArgumentException(IAE)
- JVM exceptions: Exceptions are referred to as those exceptions which are raised automatically by the JVM (Java Virtual Machine) whenever a particular event occurs. Examples are ArrayIndexOutOfBoundsException and NullPointException(NPE).
The top 5 exceptions that occur most are as follows:
It is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
The ArrayIndexOutOfBoundsException is a Runtime Exception thrown only at runtime. The Java Compiler does not check for this error during the compilation of a program.
StackOverflowError is an error which Java doesn’t allow to catch, for instance, stack running out of space, as it’s one of the most common runtime errors one can encounter. The main cause of the StackOverflowError is that we haven’t provided the proper terminating condition to our recursive function or template, which means it will turn into an infinite loop.
This exception is thrown when forcefully string is been converted to numeric value but the format of the input string is not supported. For example, parsing a string to an integer where NULL is assigned in the string throwing unchecked exceptions.
Null is a special value used in Java. It is mainly used to indicate that no value is assigned to a reference variable. It is a runtime Exception where a special null value can be assigned to an object reference. NullPointerException is thrown when a program attempts to use an object reference that has the null value.
Note:: There are two more exceptions that do occurs frequently
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.