free estadisticas Saltar al contenido

Difference between error and exception in Java

febrero 4, 2020

"Throwable" acts as root for the Java error and the hierarchy of exceptions. "Error" is a critical condition that cannot be handled by the program code. "Exception" the exceptional situation that can be handled by the program code. The significant difference between error and exception that a error caused by lack of system resources e an exception caused by the code. Let us study other differences between error and exception together with a comparison chart.

Comparative chart

Basis for the comparison ErrorErception
BasicAn error caused by a lack of system resources.An exception caused by the code.
RecoveryAn unrecoverable mistake.A recoverable exception.
wordsThere is no way to handle an error from the program code.Exceptions are handled using three keywords "try", "catch" and "throw".
consequencesWhen the error is detected, the program ends abnormally.When an exception is caught, it is thrown and captured by the keywords "throw" and "catch" correspondingly.
typesErrors are classified as uncontrolled type.Exceptions are classified as popped or uncontrolled type.
PackageIn Java, errors are referred to as the "java.lang.Error" package.In Java, exceptions are defined in "java.lang.Exception".
ExampleOutOfMemory, StackOverFlow.Checked exceptions: NoSuchMethod, ClassNotFound. Unchecked exceptions: NullPointer, IndexOutOfBounds.

Definition of error

" Error "a subclass of the built-in class" Throwable ". Errors are critical conditions that occur due to lack of system resources and cannot be handled by program code. Errors cannot be recovered by any means because they can be created, launched, captured or replicated. Errors are caused due to a catastrophic error that usually cannot be handled by the program.

The errors are always of the uncontrolled type, as the compiler has no knowledge of its occurrence. Errors always occur in the runtime environment. The error can be explained with the help of an example, the program has a stack overflow error, an insufficient memory error or a system crash error, this type of error due to the system. The code is not responsible for these errors. The consequence of the error that the program is terminated abnormally.

Definition of exception

"Exception" also a subclass of built-in class "Throwable". Exceptions are exceptional conditions that occur in a runtime environment. Most of the time exceptions are caused by our program code. But exceptions can be handled by the program itself, as exceptions are recoverable. Exceptions are handled using three keywords "try", "catch", "throw". The syntax of writing an exception:

 prova {// scrivi qui il tuo codice} Catch (tipo di eccezione) {// scrivi qui il tuo codice} 

In the previous code, the code written in the try block is the code you want to monitor for the exception. If the exception occurs in a try block, it is thrown using the "throw" keyword. The thrown exception can be caught by the "catch" block of the previous code. "Exception type" the type of exception that occurred.

In simple words we can say that errors that occurred due to improper code are called exceptions. For example, if a requested class is not found or a requested method is not found. These types of exceptions are due to the code in the program; the system is not responsible for these types of exceptions. Exceptions are classified as "checked" and "unchecked". Unchecked exceptions are not aware of the compiler as they occur during runtime, while the compiler has knowledge of checked exceptions as they are known to the compiler during compilation.

Key differences in error and exception

  1. The error occurs only when the system resources are insufficient while, an exception is thrown if a code presents some problem.
  2. An error can never be recovered while, an exception can be recovered by preparing the code to handle the exception.
  3. An error can never be handled, but an exception can be handled by code if the code that throws an exception is written inside a try and catch block.
  4. If an error has occurred, the program will terminate abnormally. On the other hand, if an exception occurs, the program throws an exception and is handled using the try and catch block.
  5. The errors are of the uncontrolled type, that is, the error not known to the compilers, while an exception classified as selected and deselected.
  6. The errors are defined in the java.lang.Error package while an exception is defined java.lang.Exception.

Conclusion:

The exceptions are the result of errors made in the coding of the program and the errors are the result of the malfunction of the system

Rate this post