Java defines a class called Exception, which represents generic unexpected events.
Subclasses define specific unexpected events. There are two general categories of exceptions:
- checked and
- unchecked.
Unchecked exceptions descend from RuntimeException and Error, which are subclasses of Exception. You should leave unchecked exceptions to Java.
That is, you should always throw checked exceptions, never subclasses of RuntimeException or Error.
You can look through the APIs for the different types of checked and unchecked exceptions that Java defines.
The difference between a checked and unchecked exception is that you do not have to wrap the call to a method that might throw an unchecked exception in a try/catch block, while you must do so for a checked exception. But unchecked exceptions will halt your thread in the same manner as checked exceptions if you do not catch them. Here is a listing of some of the more
common exceptions in Java and where they might occur:
A quick way to determine who should throw an exception is to see if the exception extends java.lang.Error.
Errors are always thrown only by the JVM. Generally, RuntimeExceptions are also thrown by the JVM. However, it is acceptable for an application code to throw a RuntimeException if it makes sense for the application to throw a RuntimeException in a given situation.