4

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.

Asif Mushtaq
  • 3,658
  • 4
  • 44
  • 80
  • Possible duplicate of [Java: checked vs unchecked exception explanation](http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation) – hotzst Mar 19 '16 at 07:45
  • @AndyTurner what does mean by recoverable? – Asif Mushtaq Mar 19 '16 at 07:50
  • @hotzst why duplicate? my question is about recover-able and non-recoverable. – Asif Mushtaq Mar 19 '16 at 07:52
  • 1
    checked exceptions are also referred to recoverable in the sense that scenarios can be defined when they happen. For un-check exceptions and Errors this is not the case therefore non-recoverable. – hotzst Mar 19 '16 at 07:57
  • @hotzst can you give example of recoverable and non-recoverable? – Asif Mushtaq Mar 19 '16 at 07:59
  • the exception classing is a nightmare imo. im reading this more than 10 minutes and i still can't define what they mean by "recoverable" – dtc Aug 07 '19 at 20:44
  • Does this answer your question? [Differences between Exception and Error](https://stackoverflow.com/questions/912334/differences-between-exception-and-error) – Shivam Puri Aug 28 '21 at 14:52

4 Answers4

7

Error, as you already figured out, means you are in serious trouble. In a catch block you might be able to do something like logging, but basically that's it.

Non-recoverable exceptions are mostly runtime exceptions like NullPointerException. They are usually the result of some missed checks in the program code. Therefore the solution is normally to fix the code.

A recoverable exception is something that you know beforehand can happen and take certain measures. Think of a web application that calls some backend service. That service may or may not be available which can cause the execution of the operation to fail. Therefore you have a checked exception, in this case most likely a custom exception that you throw, and then handle it in the front end code in a manner where you tell the user, sorry backend service xy is down, try again later or contact support.

Recoverable does not mean that the application can do something to resolve the cause of the exception, though there may be cases where this is possible.

Beks_Omega
  • 404
  • 1
  • 5
  • 13
hotzst
  • 7,238
  • 9
  • 41
  • 64
0

All classes that inherit from class Exception but not directly or indirectly from class RuntimeException are considered to be checked exceptions.Such exceptions are typically caused by conditions that are not under the control of the program. Example

  • in file processing, the program can’t open a file if it does not exist.

Recoverable So It is very easy to know that if a file does not exists so you dont need to open that file hence that is recoverable

All exception types that are direct or indirect subclasses of RuntimeException (package java.lang) are unchecked exceptions. These are typically caused by defects in program’s code. Example

  • ArrayIndexOutOfBoundsExceptions
  • ArithmeticExceptions
  • Error

Unrecoverable So thatswhy program can not recover from these kind of errors or exceptions

Muhammad Ali
  • 179
  • 1
  • 10
0

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.

snnsnn
  • 10,486
  • 4
  • 39
  • 44
0

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.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 24 '22 at 16:25