In this post, we will learn the difference between Errors and Exceptions in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Difference between Errors and Exceptions in Java
Features | Errors | Exceptions |
---|---|---|
Type | Errors in Java are of type java.lang.Error. | Exceptions in Java are of type java.lang.Exception. |
Unchecked or Checked | All errors in Java are unchecked type. | Exceptions include both checked as well as unchecked types. |
Known to Compiler | Errors happen at runtime. They will not be known to the compiler. | Checked exceptions are known to the compiler whereas unchecked exceptions are not known because they occur at runtime. |
Recovery | It is impossible to recover from errors. | You can recover from exceptions by handling them through try-catch blocks. |
Causes | Errors are mostly caused by the environment in which the application is running. | Exceptions are mainly caused by the application itself. |
Examples | Examples: java.lang.StackOverflowError, java.lang.OutOfMemoryError | Examples: Checked Exceptions: SQLException, IOException Unchecked Exceptions: ArrayIndexOutOfBoundException, ClassCastException, NullPointerException |
Collection Framework
Interview Questions
Java
X vs Y
Comments
Post a Comment