- Exception and Error both are sub classes of java.lang.Throwable class.
- We can handle Exceptions at runtime but Errors we can not handle.
- Exceptions are the objects representing the logical errors that occur at run time and makes JVM enters into the state of "ambiguity".
- The objects which are automatically created by the JVM for representing these run time errors are known as Exceptions.
- An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.
- An error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Examples of errors include OutOfMemoryError, StackOverflowError, and NoClassDefFoundError. An exception, on the other hand, is a subclass of Throwable that indicates conditions that a reasonable application might want to catch. Examples of exceptions include IOException, SQLException, and NullPointerException.
- Exceptions are used to indicate that a problem occurred that the application can handle, while errors indicate that a more serious problem has occurred that the application cannot handle.
- Exceptions are events that are triggered by the code you write, such as a null pointer reference or a file that cannot be found. When an exception occurs, the program will stop executing and the control will be transferred to an exception handler. You can catch exceptions using try-catch blocks and handle them appropriately.
- Errors, on the other hand, are typically caused by external factors such as the JVM running out of memory, or the system running out of resources. These types of errors are not typically recoverable by your application and are not meant to be handled by you.
- It is generally recommended to handle exceptions, as they are often the result of programming mistakes and can be corrected in the code. Errors, on the other hand, should be logged and reported to the user, but the program should not try to recover from them.
- In summary, Errors are serious problems that are not usually handled by the application and are thrown by the JVM when it encounters a problem that it can't handle, like running out of memory, stack overflow, etc. While exceptions are events that are triggered by the code you write or external libraries, and can be handled by the application using try-catch blocks, to handle the exceptional events in an appropriate way.
Difference Between Exceptions and Errors
- If exception occurs we can handle it by using try and catch block. If Error occurs we can noyt handle it , program execution will be terminated.
- In Exception we have two types
1. Checked Exception
- Error are by default unchecked exceptions.
- Exceptions are related to application where ad Error are related to environment in which application is running.
- Exception are of type java.lang.Exception
- Errors are of type java.lang.Error
- Error will run at run time.
- In Exceptions Checked Exceptions will known to compiler so we need to handle these exceptions at compile time itself otherwise compile time Error will come.
- Unchecked Exception will come at run time need to handle by using try and catch blocks.
Read More About Exceptions here:
- Exception Handling Introduction
- try catch finally in Java
- User Defined Exceptions in Java
- Throw vs throws
- Final vs finally vs finalize()