Exceptions in Java I: Introduction

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 –

  1. User entering float where integer was expected
  2. Being asked to read a file that does not exist
  3. Being asked to invoke a method on an object that has still not been initialized
  4. Being asked to invoke a method that does not exist in class definition
  5. Trying to access an array element at an index beyond the size of the array
  6. Calling so many methods that Java stack fills up
  7. Allocating so many objects that you run out of heap space
  8. Defining so many type definitions and static constructs that JVM runs out of PermGen space
  9. Trying to divide your bank account balance by zero (in hopes of making it tend toward  )
  10. 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 JavaDocDescription
NullPointerExceptionYou don’t have any money. You decide to shop anyway.
SecurityExceptionYou are now on the no-fly (array) list. Try to catch that next bus or train.
IllegalAccessExceptionYou have been caught invading homes, peeping inside windows and burglarizing
NoSuchMethodExceptionTime to stop trying your lunatic methods and moves
ArithmeticExceptionYou are exceptionally poor in arithmetic. Stop wondering when you are going to get GeometryException and AlgebraException
IndexOutOfBoundsExceptionYou try to put wedding ring on the 11th finger
IllegalArgumentExceptionYou make an exceptionally invalid and idiotic point
ClassNotFoundExceptionYou are usually late to school. On the exceptional/rare occasion that you are not, class is cancelled.
InterruptedExceptionYou learn on your wedding night that FBI has issued a BOLO – be on lookout – notice for you
InvalidClassExceptionAt 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.

Leave a comment

Your email address will not be published.