Difference between Error and Exception

Key difference:  Both, error and exceptions are derived java.lang.Throwable problems. An ‘error’ is a serious problem that cannot be recovered once occurred, whereas an ‘exception’ is a problem which can be handled and corrected after execution.

An ‘error’ in Java is a serious problem, which once occurred cannot be handled and recovered. An error is defined as “any departure from the expected behavior of the system or program, which stops the working of the system”. It comes under the Throwable class, which categorizes it as a serious and reasonable application. After its execution, this application re-occurs several times, creating an interruption in the systems functions. There are several subclasses under the error form. These errors occur at specific levels and functions, and are handled by specific functions. In a broader way, these errors can be broken into two categories: Design-time errors and Logical errors.

Basically, there are two main types of errors and their conditions:

  • Run-time error: These are executed at the running of the program.
  • Compile-tile error: These are executed at the compiler, during the compiling time.

Further these types are subdivided into other forms.

For example:

class MyClass {

void f() {

int n = 10; //Error, closing brace is missing//

void g() {

int m = 20;

}

}

An ‘exception’ in Java is a problem which can be handled and caught. It is basically defined as “any error or problem which one can handle and continue to work normally”. It is a runtime error, which can be corrected and handled at the time of execution. Exceptions mostly occur during the execution of any program. It may occur due to any reason, such as a user has entered invalid data, a file that needs to be opened but cannot be found, a network connection that has been lost in the middle of communications and when the JVM has run out of memory. Some of these exceptions are caused by user errors, while others by programmer error and by physical resources that have failed in some manner. The java.lang.Exception class consists of all the exception subclasses. These exception classes are subclasses of the Throwable class. The exceptions are handled and caught by the exception method techniques. There are various exception methods and techniques which handle different types of exceptions.

In general, there are the following types of exceptions:

  • Checked exceptions: It is an exception that is typically a user error or a problem that cannot be foreseen by the programmer.
  • Runtime exceptions: It is an exception that occurs that probably could have been avoided by the programmer.

For example:

try {

int x = 10;

int y = 0;

int z = x / y;

System.out.println( z );

}

catch ( Exception err ) {

System.out.println( err.getMessage( ) );

}

Comparison between Errors and Exception:

 

Errors

Exception

Indicates

It indicates serious problems that a reasonable application should not try to catch.

It indicates conditions that a reasonable application might want to catch.

Nature

They are often fatal in nature and recovery.

They are not fatal in all the cases.

Recovery

They cannot be recovered once occurred.

They are generally those problems from which a program can recover.

Handled or not handled

Error is not meant to be caught.

Exception is meant to be caught.

Caused by

These indicate a system error or a problem with a low-level resource.

These are caused by a programmer.

Co-relation

Errors are also unchecked exception.

Exceptions are also Run-Time errors.

Should be handled at the levels

Errors should be handled at the system level, if possible.

Exception should be handled at the application level.

Types

Error is generally divided into two types e.g.:

Run-Time

Compile-time errors.

Exception is generally divided into two categories e.g.

Checked and

Unchecked exceptions.

Examples

java.lang.OutOfMemoryError

IOException

Image Courtesy: netmechanic.com, ntu.edu.sg

Most Searched in Arts and Humanities Most Searched in Sports
Most Searched in Entertainment and Music Most Searched Non-Alcoholic Drinks
Many vs Several
Language vs Script
Nokia Lumia 928 vs Blackberry Z10
Of vs Off

Add new comment

Plain text

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.