An exception is a condition that occurs in a program’s control flow which prevents further execution of the current code path. Exceptions represent occurrence of a special events and allow you to respond to those special situations as you deem appropriate. Don’t be under the false impression that exceptions are just error handling constructs. Or that they are used to prevent runtime problems. Exceptions are more than that. They are Java’s mechanism for handling special events. Errors happen to be a type of the special event. JVM about to terminate may be another. Exceptions allow you to respond to both these and many more types of situations. Certainly, JVM about to shutdown is not necessarily an error. After all, all JVMs are bound to shutdown at some point.
What are Java Exceptions?
As explained in the introductory paragraph, exceptions are, well exceptional situations. Java Exceptions are Java objects that encapsulate the special situation they represent. Error happens to be the most common special situation.
You may come across statements such as “Exceptions are used for error handling”. That’s only partly true. It’s like saying “Prof. Donald Knuth’s books are used for getting jobs”. Certainly, The Art of Computer Programming books are a great tool for getting a job and harassing your interviewer at the same time, but those great books have a broader purpose. You can be pretty sure that Prof. Knuth did not intend to make them a job interview tool.
Java exceptions are objects that are created either by JVM or by user code and represent a situation that warrants special handling. Some examples of such exceptional situations are –
- User entering float where integer was expected
- Being asked to read a file that does not exist
- Being asked to invoke a method on an object that has still not been initialized
- Being asked to invoke a method that does not exist in class definition
- Trying to access an array element at an index beyond the size of the array
- Calling so many methods that Java stack fills up
- Allocating so many objects that you run out of heap space
- Defining so many type definitions and static constructs that JVM runs out of PermGen space
- Trying to divide your bank account balance by zero (in hopes of making it tend toward ∞ )
- Multiplying the name of your local politician by zero (hoping that Java would act like Python and the beast disappears)
Java Humor: Frequently Encountered Exceptions
Exceptions are pretty unexceptional in the life of a Java programmer. You are likely to see these at one point or the other –
|Exception and its JavaDoc||Description|
|NullPointerException||You don’t have any money. You decide to shop anyway.|
|SecurityException||You are now on the no-fly (array) list. Try to catch that next bus or train.|
|IllegalAccessException||You have been caught invading homes, peeping inside windows and burglarizing|
|NoSuchMethodException||Time to stop trying your lunatic methods and moves|
|ArithmeticException||You are exceptionally poor in arithmetic. Stop wondering when you are going to get GeometryException and AlgebraException|
|IndexOutOfBoundsException||You try to put wedding ring on the 11th finger|
|IllegalArgumentException||You make an exceptionally invalid and idiotic point|
|ClassNotFoundException||You are usually late to school. On the exceptional/rare occasion that you are not, class is cancelled.|
|InterruptedException||You learn on your wedding night that FBI has issued a BOLO – be on lookout – notice for you|
|InvalidClassException||At the end of the lecture you realize you are in the wrong lecture theatre|
Java Exceptions is a vast topic and in order to do justice to the topic, we will go through it in multiple lessons. The next lesson covers the class hierarchy of exceptions.