What does mean by recoverable and un-recoverable exception or error

Viewed 9278

I'm trying to understand difference between error and exception but it looks same thing and on Oracle Official tutorials I read this line.

Checked exceptions are subject to the Catch or Specify Requirement. All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses.

Now about I'm thinking it's same. But after searching more I found some difference as theoretical that.

Exception: are recoverable

Errors: not recoverable.

Exception Example:

try{
  read file
}
catch(FileNotFoundException e){
// how I can recover here? can i create that file?
// I think we can just make log file to continue or exit.
}

Error Example:

try{
      try to locate memory
}
catch(MemoryError e){   
    // I think we can just make log file to continue or exit.
}

Edited I'm asking about recover-able and non-recoverable.

4 Answers

Unrecoverable errors are the ones that put the application in an undefined state, like a broken database connection or a closed port, you may handle the error and continue to execute but it would not make sense. Modern languages like Rust and Go use the name panic for errors of these nature, to make the distinction clear. Best action to take is logging the error, if it is possible, and exiting the application.

A recoverable errors are the ones we can handle gracefully, like division by zero or validation errors. It is something expected and the resulting behavior is covered in the language spec. Yes, application behaves erratic when that a recoverable error happens but we can contain it or work around it.

The Object Throwable has 2 childs : Exception and Error.

All Exceptions are recoverable and all Errors are non-recoverable.

All Exceptions are recoverable because you can catch them and let your program continue its execution.

However all Errors , even when you add them to a catch block, will cause the abrupt termination of your program.

Examples of Errors: StackOverflowError, OutOfMemoryError,..

Remark : Checked and unchecked Exceptions are childs of Exception so recoverable.

Related