199

Possible Duplicate:
Differences betweeen Exception and Error

How can I differentiate between Errors and Exceptions in Java?

1
  • 1
    In general I think that if you catch an error you stop the program from running, but with exception you can branch your control flow
    – themhz
    Sep 1, 2021 at 13:33

4 Answers 4

293

An Error "indicates serious problems that a reasonable application should not try to catch."

while

An Exception "indicates conditions that a reasonable application might want to catch."

Error along with RuntimeException & their subclasses are unchecked exceptions. All other Exception classes are checked exceptions.

Checked exceptions are generally those from which a program can recover & it might be a good idea to recover from such exceptions programmatically. Examples include FileNotFoundException, ParseException, etc. A programmer is expected to check for these exceptions by using the try-catch block or throw it back to the caller

On the other hand we have unchecked exceptions. These are those exceptions that might not happen if everything is in order, but they do occur. Examples include ArrayIndexOutOfBoundException, ClassCastException, etc. Many applications will use try-catch or throws clause for RuntimeExceptions & their subclasses but from the language perspective it is not required to do so. Do note that recovery from a RuntimeException is generally possible but the guys who designed the class/exception deemed it unnecessary for the end programmer to check for such exceptions.

Errors are also unchecked exception & the programmer is not required to do anything with these. In fact it is a bad idea to use a try-catch clause for Errors. Most often, recovery from an Error is not possible & the program should be allowed to terminate. Examples include OutOfMemoryError, StackOverflowError, etc.

Do note that although Errors are unchecked exceptions, we shouldn't try to deal with them, but it is ok to deal with RuntimeExceptions(also unchecked exceptions) in code. Checked exceptions should be handled by the code.

11
  • 14
    RuntimeExceptionS are usually caused by bad programming.
    – wulfgarpro
    Sep 9, 2012 at 8:46
  • 10
    On the contrary, wulfgar, my experience tells me RuntimeExceptions are the result of invalid user input.
    – corsiKa
    Apr 19, 2015 at 4:45
  • 26
    @corsiKa invalid user input should be checked by the programmer.
    – mochomecha
    May 25, 2016 at 8:19
  • 3
    I'm guessing @corsiKa was being ironic Jan 31, 2017 at 15:23
  • 3
    @mochomecha Yes, yes it should be. And when it can't be recovered from, it throws some subclass of RuntimeException. That's literally what the class is for: signaling an invalid user input.
    – corsiKa
    Jan 31, 2017 at 16:16
24

Error and Exception both extend Throwable, but mostly Error is thrown by JVM in a scenario which is fatal and there is no way for the application program to recover from that error. For instance OutOfMemoryError.

Though even application can raise an Error but its just not a good a practice, instead applications should use checked exceptions for recoverable conditions and runtime exceptions for programming errors.

1
  • 2
    I may have an example where you may want to try to recover from Error (OutOfMemoryError for example). Let say you have a mobile application and you want to put a background picture. On some old or low memory devices this may not be possible (not enoght memory left for the application itself). So there are 2 options you will check if its possible every time you want to display such picture or you just try to display it and if it will fail you catch the OutOfMemoryError and continue without it.
    – Jan Kubovy
    Dec 11, 2015 at 7:08
18

Error is something that most of the time you cannot handle it.

Exception was meant to give you an opportunity to do something with it. like try something else or write to the log.

try{
  //connect to database 1
}
catch(DatabaseConnctionException err){
  //connect to database 2
  //write the err to log
}
11

In general error is which nobody can control or guess when it occurs.Exception can be guessed and can be handled. In Java Exception and Error are sub class of Throwable.It is differentiated based on the program control.Error such as OutOfMemory Error which no programmer can guess and can handle it.It depends on dynamically based on architectire,OS and server configuration.Where as Exception programmer can handle it and can avoid application's misbehavior.For example if your code is looking for a file which is not available then IOException is thrown.Such instances programmer can guess and can handle it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.