197

Possible Duplicate:
Differences betweeen Exception and Error

How can I differentiate between Errors and Exceptions in Java?

Community
  • 1
  • 1
hiren gamit
  • 2,083
  • 6
  • 25
  • 23
  • 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 01 '21 at 13:33

4 Answers4

291

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.

Adil
  • 4,503
  • 10
  • 46
  • 63
Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46
  • 14
    `RuntimeException`S are usually caused by bad programming. – wulfgarpro Sep 09 '12 at 08:46
  • 10
    On the contrary, wulfgar, my experience tells me `RuntimeException`s are the result of invalid user input. – corsiKa Apr 19 '15 at 04:45
  • 26
    @corsiKa invalid user input should be checked by the programmer. – mochomecha May 25 '16 at 08:19
  • 3
    I'm guessing @corsiKa was being ironic – Bradley Thomas Jan 31 '17 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 '17 at 16:16
  • @corsiKa The RuntimeException class has a more general purpose than signaling invalid user input. From the official Oracle Docs: _RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine._ This includes a lot of things, for example out of memory Exceptions, connection exceptions and so on. – mochomecha May 11 '17 at 09:12
  • 2
    @mochomecha I'm confused. Out of memory is an Error, not an Exception. That's even harder to recover from. And a break in a connection is just another form of bad user input. There is just the user, the program, and the environment. If the environment breaks, it's an error. If the program breaks, it's an exception. If the user breaks, it's a runtime exception. Pretty darned simple stuff. – corsiKa May 14 '17 at 17:16
  • @Corsika FYI, an example of an Exception related to the connection, which is not motivated by the user input: java.net.NoRouteToHostException. It is pretty darned simple. – mochomecha May 14 '17 at 18:44
  • @mochomecha Sure. NoRouteToHostException isn't a runtime exception though - it's a subclass of IOException which has to be explicitly caught... So while I was inaccurate about connections being input and not environment, that kind of makes my point for me. – corsiKa May 15 '17 at 15:42
  • 1
    If "OutOfMemory" error shouldn't be `try catched` then how is a good programmer suppose to account for this situation? – Matthew Jan 29 '19 at 17:34
  • @Matthew by ensuring the system does not use more memory than is available on the platform where it is running – leonardost Jul 31 '20 at 15:38
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.

Jugal Shah
  • 3,621
  • 1
  • 24
  • 35
  • 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 '15 at 07: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
}
fatnjazzy
  • 6,070
  • 12
  • 57
  • 83
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.

nicks
  • 2,161
  • 8
  • 49
  • 101