Differences between Exception and Error in java

java.lang.Error and java.lang.Exception both are subclass of java.lang.Throwable class.

You can not recover error but you can recover exception by using try-catch block. Errors are mostly caused by the environment in which application is running. But Exception are mainly caused by the application itself.

Given below the difference between exception and error

Errors:

  • Errors in java are of type java.lang.Error.
  • All errors in java are unchecked type.
  • Errors happen at run time. They will not be known to the compiler.
  • It is impossible to recover from errors.
  • Errors are mostly caused by the environment in which application is running.
  • Examples : java.lang.StackOverflowError, java.lang.OutOfMemoryError

Exceptions:

  • Exceptions in java are of type java.lang.Exception.
  • Exceptions include both checked as well as unchecked type.
  • Checked exceptions are known to the compiler whereas unchecked exceptions are not known to the compiler because they occur at run time.
  • You can recover from exceptions by handling them through try-catch blocks.
  • Exceptions are mainly caused by the application itself.
  • Examples : Checked Exceptions : SQLException, IOException
  • Unchecked Exceptions : ArrayIndexOutOfBoundException, ClassCastException, NullPointerException

Hope you got the clear idea!