Exceptions and Errors in Java
In this article, we are going to discuss exceptions and errors Java.
Exception
Exception are something that disrupts the normal flow of application. Once an exception is occurred, the statement after that are not executed.
Types of Exception
Exception can happen in both compile time and run time. There are 2 types of Exception in Java.
- Checked Exception
These exceptions arise during compilation, thus handling them there is necessary. Exceptions that are children of the Exception class and do not extend the RuntimeException class are known as checked exceptions.
Whenever we are reading a file in our java application, we need to handle FileNotFoundException to handle cases where files are not found. Compiler force us to handle such checked Exceptions.
Examples of Checked Exceptions are:- SQLException, IOException, etc
- Unchecked Exception
These types of exception are occurred in Runtime. RuntimeException and its sub classes are unchecked exceptions. Runtime Exceptions are not required to be handled at compile time.
Examples of Unchecked Exceptions are:- NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException, NumberFormatException, etc.
Exception Hierarchy
Throwable class is the parent class of all exceptions. Subclasses of the Throwable class include the Exception and Error classes. Then, exception is divided into Checked and Unchecked Exception categories. When handling exceptions in Java, we can utilize throws and try-catch blocks.
Errors
Errors are occurred in Runtime. They normally occurs due to lack of system resources or fault in environment they are running. Error is thrown by JVM in a scenario which is fatal and there is no way for the application program to recover from that error.
Since Error is sub class of Throwable, we can catch Throwable but it is not advised to catch error since error are serious issue in the system, instead we need to understand the error and fix the code.
Get Ayush Basnet’s stories in your inbox
Join Medium for free to get updates from this writer.
Examples of Errors are: StackOverFlowError, OutOfMemoryError, etc.
Conclusion
Exception and Errors both are sub class of Throwable class. Exception can occur in both compile time and runtime whereas errors mostly occur in runtime. We can use exception handling mechanism to handle exceptions whereas Error are not suggested to be handled.
Thank you for reading this article. I appreciate your time and interest. Your feedback is valuable to me, so please feel free to share your thoughts. Stay tuned for more articles!
Let’s stay connected! Feel free to connect with me on my social media profiles. Here are some ways you can reach out to me:
- Connect with me on LinkedIn: linkedin.com/in/ayushbasnet
- Check out my Github profile: github.com/ayushjung63










