Java Exception Types
Throwable
, Error
, Exception
and RuntimeException
are classes with special meaning in the Java language. The compiler imposes different constraints on these classes when used in throw
statements and catch
blocks. The classes have the following inheritance hierarchy:
Throwable
In Java you can only throw
and catch
objects of type, or subtype of, Throwable
.
The only Throwable
subclasses provided by the Java API are Error
and Exception
.
Throwable
is regarded as a checked exception.
Error
An Error
is thrown to indicate a serious problem that the application should not try to resolve.
Error
and all its subclasses are regarded as unchecked exceptions.
-
StackOverflowError
, typically due to infinite recursion -
OutOfMemoryError
-
AssertionError
-
NoClassDefFoundError
, typically due to a classpath configuration error -
NoSuchMethodError
/NoSuchFieldError
, typically due to wrong version of a class being loaded
Exception
Exception
s are used for conditions that a reasonable application might want to catch.
Exception
and all it's subclasses (except RuntimeException
) are regarded as checked exceptions.
-
InterruptedException
, thread was interrupted while in a blocking call -
IOException
, top level exception for all I/O related errors -
EOFException
-
FileNotFoundException
-
UnknownHostException
, check your internet connection :)
RuntimeException
The RuntimeException
is a subclass of Exception
. It is special because it is an unchecked exception.
RuntimeException
s are used for conditions that an application typically don't catch, such as programming errors.
-
ClassCastException
, invalid cast -
NullPointerException
-
ArithmeticException
, typically due to a division by zero -
IndexOutOfBoundsException