Java Error

An error defines a reasonable issue that is topping the execution of the program. In different programming languages, there occur different types of errors as per the concepts.

This section will discuss errors in Java and different types of errors, and when such errors occur.

What is an Error in Java

In Java, an error is a subclass of Throwable that tells that something serious problem is existing and a reasonable Java application should not try to catch that error. Generally, it has been noticed that most of the occurring errors are abnormal conditions and cannot be resolved by normal conditions. As these errors are abnormal conditions and should not occur, thus, error and its subclass are referred to as Unchecked Exceptions. In Java, we have the concept of errors as well as exceptions. Thus there exist several differences between an exception and an error. Errors cannot be solved by any handling techniques, whereas we can solve exceptions using some logic implementations. So, when an error occurs, it causes termination of the program as errors cannot try to catch.

Certain Errors in Java

The Java.lang.Errors provide varieties of errors that are thrown under the lang package of Java. Some of the errors are:

Error NameDescription
AbstractMethodErrorWhen a Java application tries to invoke an abstract method.
ErrorIndicating a serious but uncatchable error is thrown. This type of error is a subclass of Throwable.
AssertionErrorTo indicate that an assertion has failed.
ClassCircularityErrorWhile initializing a class, a circularity is detected.
IllegalAccessErrorA Java application attempts either to access or modify a field or maybe invoking a method to which it does not have access.
ClassFormatErrorWhen JVM attempts to read a class file and find that the file is malformed or cannot be interpreted as a class file.
InstantiationErrorIn case an application is trying to use the Java new construct for instantiating an abstract class or an interface.
ExceptionInInitializerErrorSignals that tell an unexpected exception have occurred in a static initializer.
InternalErrorIndicating the occurrence of an unexpected internal error in the JVM.
IncompatibleClassChangeErrorWhen an incompatible class change has occurred to some class of definition.
LinkageErrorIts subclass indicates that a class has some dependency on another data.
NoSuchFieldErrorIn case an application tries to access or modify a specified field of an object, and after it, that object no longer has this field.
OutOfMemoryErrorIn case JVM cannot allocate an object as it is out of memory, such error is thrown that says no more memory could be made available by the GC.
NoClassDefFoundErrorIf a class loader instance or JVM, try to load in the class definition and not found any class definition of the class.
ThreadDeathIts instance is thrown in the victim thread when in thread class, the stop method with zero arguments is invoked.
NoSuchMethodErrorIn case an application tries to call a specified method of a class that can be either static or instance, and that class no longer holds that method definition.
StackOverflowErrorWhen a stack overflow occurs in an application because it has recursed too deeply.
UnsatisfiedLinkErrorIn case JVM is unable to find an appropriate native language for a native method definition.
VirtualMachineErrorIndicate that the JVM is broken or has run out of resources, essential for continuing operating.
UnsupportedClassVersionErrorWhen the JVM attempts to read a class file and get to know that the major & minor version numbers in the file are unsupportable.
UnknownErrorIn case a serious exception that is unknown has occurred in the JVM.
VerifyErrorWhen it is found that a class file that is well-formed although contains some sort of internal inconsistency or security problem by the verifier.

Let's see an example implementation to know how an error is thrown.

Java Error Example

Below is the error example implementation code of the occurrence of the System crash:

Code Explanation

  • In the above code, we have performed a program of Stack overflow.
  • In it, we have created a class, namely StackOverflow, within which designed a function that performs an infinite recursion.
  • Next, in the main class, we have invoked the function, and it results in infinite recursion.

On executing the code, we got the below-shown output:

Java Error

In order to stop the infinite execution, we may require to terminate the program because it will not be tackled by any normal condition.

Error Vs. Exception

Java Error

There are the below points that differentiate between both terms:

ExceptionError
Can be handledCannot be handled.
Can be either checked type or unchecked typeErrors are of unchecked type
Thrown at runtime only, but the checked exceptions known by the compiler and the unchecked are not.Occurs at the runtime of the code and is not known to the compiler.
They are defined in Java.lang.Exception package.They are defined in Java.lang.Error package
Program implementation mistakes cause exceptions.Errors are mainly caused because of the environment of the program where it is executing.

Next TopicJava Apps




Latest Courses