Javatpoint Logo
Javatpoint Logo

Exception Vs Error in Java

The general meaning of exception is a deliberate act of omission while the meaning of error is an action that is inaccurate or incorrect. In Java, Exception, and Error both are subclasses of the Java Throwable class that belongs to java.lang package. But there exist some significant differences between them. So, in this section, we are going to discuss the key differences between exception and error.

Exception Vs Error in Java

Before moving ahead in this section let's have a look at the hierarchy of the Java Throwable class.

Exception Vs Error in Java

Exception

The term exception is shorthand for the phrase exception event. It is an event that occurs during the execution of the program and interrupts the normal flow of program instructions. These are the errors that occur at compile time and run time. It occurs in the code written by the developers. It can be recovered by using the try-catch block and throws keyword. There are two types of exceptions i.e. checked and unchecked.

There are some important points that should be kept in mind while dealing with the exception:

  • When an error is detected, an exception is thrown.
  • Any exception that is thrown must be caught by the exception handler.
  • If the programmer has forgotten to provide an exception handler, the exception will be caught by the catch-all exception handler provided by the system.
  • Exception may be rethrown if exception handler is failure to handle it.

Advantages of Exceptions

  • It separates error handling code from regular code.
  • It has the ability to propagate error reporting up the call stack of methods.
  • The grouping or categorizing of exceptions is a natural outcome of the class hierarchy.

Let's understand the exception through a Java program.

Example of Exception

ExceptionExample.java

Let's run the above program and enter a float value deliberately to generate an exception.

Exception Vs Error in Java

It shows the InputMismatchExaception. Because the program accepts an integer value. We observe that the next statement is skipped and the program is terminated.

Error

Errors are problems that mainly occur due to the lack of system resources. It cannot be caught or handled. It indicates a serious problem. It occurs at run time. These are always unchecked. An example of errors is OutOfMemoryError, LinkageError, AssertionError, etc. are the subclasses of the Error class.

Let's understand the error through a Java program.

Example of Error

ErrorExample.java

Output:

Exception Vs Error in Java

We observe that on running the program, we get the StackOverflowError, not an exception.

Let's discuss the key differences between exception and error.

Difference Between Exception and Error

In Java, Error, and Exception both are subclasses of the Java Throwable class that belongs to java.lang package.

Basis of Comparison Exception Error
Recoverable/ Irrecoverable Exception can be recovered by using the try-catch block. An error cannot be recovered.
Type It can be classified into two categories i.e. checked and unchecked. All errors in Java are unchecked.
Occurrence It occurs at compile time or run time. It occurs at run time.
Package It belongs to java.lang.Exception package. It belongs to java.lang.Error package.
Known or unknown Only checked exceptions are known to the compiler. Errors will not be known to the compiler.
Causes It is mainly caused by the application itself. It is mostly caused by the environment in which the application is running.
Example Checked Exceptions: SQLException, IOException
Unchecked Exceptions: ArrayIndexOutOfBoundException, NullPointerException, ArithmaticException
Java.lang.StackOverFlow, java.lang.OutOfMemoryError






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA