Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between Errors and Exceptions? [duplicate]

Tags:

java

exception

Possible Duplicate:
Differences betweeen Exception and Error

How can I differentiate between Errors and Exceptions in Java?

like image 743
hiren gamit Avatar asked Apr 28 '11 04:04

hiren gamit


People also ask

What is the difference between errors and exceptions?

The error indicates trouble that primarily occurs due to the scarcity of system resources. The exceptions are the issues that can appear at runtime and compile time. 2. It is not possible to recover from an error.

What is the difference between an exception and an error in Java?

Errors are usually raised by the environment in which the application is running. For example, an error will occur due to a lack of system resources. Exceptions are caused by the code of the application itself. It is not possible to recover from an error.

What is the difference between error and exception in Python?

An Error might indicate critical problems that a reasonable application should not try to catch, while an Exception might indicate conditions that an application should try to catch. Errors are a form of an unchecked exception and are irrecoverable like an OutOfMemoryError , which a programmer should not try to handle.

What are the 3 types of exceptions?

There are three types of exception—the checked exception, the error and the runtime exception.


1 Answers

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.

like image 84
Nirmal- thInk beYond Avatar answered Oct 28 '22 03:10

Nirmal- thInk beYond