Exceptions: JVM v. Programatic

This is mainly for those who have a desire to follow the certification track or want to know what the author thinks. In practice, no one follows it. I am writing this as I test a pdf table stripper using OCR for automated and open source pdf extraction but I am a little worn out today and this is simple. For the record, I nearly got in a fight with an experienced developer who did not remember this since it is something to know but also know no one will use it.

Oracle makes a fairly clearer distinction between the two.

Exception Hierarchy

The exception hierarchy is clear. Every exception is a throwable. Exception is a throwable. Underneath the Exception class, checked exceptions and and Runtime or unchecked exceptions exist.

A Word From Oracle
Oracle directly states that unchecked exceptions should never be thrown or caught since it violates the principles of the language. Specifically, throwing an unchecked exception or an error “it sidesteps the intent of the catch or specify requirement and can cause problems for others using your classes” (Oracle).

Runtime exceptions cannot be expected to be caught by the compiler since they are based on program decisions rather than problems with utilizing java itself.

Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.

 
Distinguishing JVM from Programmer Exceptions
The difference between the two is the difference between what must be caught and thrown explicitly versus what must be thrown implicitly while keeping in mind the rule from Oracle.

Checked exceptions are programmer thrown exceptions, they exist just under the Exception class, extending Exception which implements the Throwable interface. Unchecked Exceptions exist under RuntimeException.

Common JVM thrown Exceptions include:

  • NullPointerException
  • IllegalArgumentException
  • NumberFormatException
  • ArithmeticException
  • SecurityException
  • IndexOutOfBoundsException<–ArrayIndexOutOfBoundsException

Common JVM thrown Errors include:

  • StackOverFlowError
  • OutOfMemoryError
  • ClassDefNotFoundError
  • NoSuchMethodError

Common Programmer thrown exceptions:

  • IOException<–SQLException,FileNotFoundException,InterruptedIOException
  • ReflectiveOperationException<–ClassNotFoundException,NoSuchMethodException
  • CloneNotSupportedException

As an aside the most specific class must be caught first in a try,catch block. Also, in reality, most programmers consider this an esoteric question between what is actually thrown and unchecked exceptions left to the JVM. Anything can be thrown without breaking the code. Oracle just does not like it and you will lose 5% of your SE7 I test grade.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s