- 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.
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()