Last Updated on 10 July 2019   |   Print Email
This article helps you understand about the core classes that make up the Java exception API as well as a list of standard errors and exceptions defined by JDK.
1. Java Exception API Hierarchy
The following diagram describes the class hierarchy of exceptions API in JDK: As you can see, Throwable is at the top of the hierarchy. It is the supertype of all exceptions and errors in Java. Below Throwable, there are 3 subtypes:
Error: represents system errors occurred in abnormal conditions. It is the base class for all errors in Java. Remember that we should not catch errors, because the program cannot be recovered in the events of errors. You may know two well-known errors are StackOverflowError and OutOfMemoryError.
Exception: is the base class for all exceptions in Java. And most of the time, we throw and catch exceptions which are sub classes of Exception.
RuntimeException: an exception of this type represents a programming error and typically we should not throw and catch runtime exceptions. NullPointerException is a very well-know runtime exception in Java.
So far we have Error, Exception and RuntimeException represent 3 different kinds of errors and exceptions in JDK. Under them, there are a lot of sub classes and their sub classes. For example, the IOException represents a general I/O error, and its subclass FileNotFoundException is a more specific exception that indicates a file cannot be located on disk. That’s how Java exception API is organized.
2. Common Errors in Java
Here’s a list of common error classes defined by JDK (most of them are under java.lang package):
AssertionError
AWTError (java.awt)
IOError (java.io)
LinkageError
ExceptionInInitializerError
NoClassDefFoundError
UnsatisfiedLinkError
VerifyError
ThreadDeath
VirtualMachineError
InternalError
OutOfMemoryError
StackOverflowError
UnknownError
3. Common Exceptions in Java
These exceptions are categorized as checked exceptions and they are grouped by package. The following is a list of common checked exceptions defined in the java.lang package:
ReflectiveOperationException
ClassNotFoundException
InstantiationException
IllegalAccessException
InvocationTargetException
NoSuchFieldException
NoSuchMethodException
CloneNotSupportedException
InterruptedException
4. Common Runtime Exceptions in Java
These exceptions are categorized as unchecked exceptions and they are grouped by package. The following is a list of common unchecked exceptions in the java.lang package:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments