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 Name | Description |
---|---|
AbstractMethodError | When a Java application tries to invoke an abstract method. |
Error | Indicating a serious but uncatchable error is thrown. This type of error is a subclass of Throwable. |
AssertionError | To indicate that an assertion has failed. |
ClassCircularityError | While initializing a class, a circularity is detected. |
IllegalAccessError | A Java application attempts either to access or modify a field or maybe invoking a method to which it does not have access. |
ClassFormatError | When JVM attempts to read a class file and find that the file is malformed or cannot be interpreted as a class file. |
InstantiationError | In case an application is trying to use the Java new construct for instantiating an abstract class or an interface. |
ExceptionInInitializerError | Signals that tell an unexpected exception have occurred in a static initializer. |
InternalError | Indicating the occurrence of an unexpected internal error in the JVM. |
IncompatibleClassChangeError | When an incompatible class change has occurred to some class of definition. |
LinkageError | Its subclass indicates that a class has some dependency on another data. |
NoSuchFieldError | In case an application tries to access or modify a specified field of an object, and after it, that object no longer has this field. |
OutOfMemoryError | In 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. |
NoClassDefFoundError | If a class loader instance or JVM, try to load in the class definition and not found any class definition of the class. |
ThreadDeath | Its instance is thrown in the victim thread when in thread class, the stop method with zero arguments is invoked. |
NoSuchMethodError | In 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. |
StackOverflowError | When a stack overflow occurs in an application because it has recursed too deeply. |
UnsatisfiedLinkError | In case JVM is unable to find an appropriate native language for a native method definition. |
VirtualMachineError | Indicate that the JVM is broken or has run out of resources, essential for continuing operating. |
UnsupportedClassVersionError | When the JVM attempts to read a class file and get to know that the major & minor version numbers in the file are unsupportable. |
UnknownError | In case a serious exception that is unknown has occurred in the JVM. |
VerifyError | When 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:
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
There are the below points that differentiate between both terms:
Exception | Error |
---|---|
Can be handled | Cannot be handled. |
Can be either checked type or unchecked type | Errors 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. |