Class Error in Java
Error is thrown from an application to indicate that some abnormal conditions happened in the application. Error class forms the root of the error hierarchy in java. Subclasses of Error should generally not be caught. It causes the termination of the code.
The Error class is the superclass of all of the standard error classes that can be thrown in Java. The subclasses of Error are normally thrown by the class loader, the virtual machine, or other support code.
Application-specific code should not normally throw any of the standard error classes.
An Error or one of its subclasses is typically thrown when an unpredictable run-time error, such as running out of memory, occurs. Because of the unpredictable nature of the events that cause errors to be thrown, a method does not have to declare the Error class or any of its subclasses in the throws clause of its method declaration.
A Java program should not try to handle the standard error classes. Most of these error classes represent nonrecoverable errors and as such, they cause the Java run-time system to print an error message and terminate program execution.
Subclasses of Error need not be declared in the throws clause of a method definition.
getMessage() method returns a message associated with the error.
The structure of the class-Error is given as:
public class java.lang.Error extends java.lang.Throwable{
//constructor
public Error();
//constructs an empty Error object that is an object with no message specified.
public Error(String message);
//constructs an Error object with the message specified.
}
The class Error also inherits methods from class Object and Throwable.
From Object class | From Throwable class |
---|---|
clone() | fillInStackTrace() |
getClass() | getMessage() |
notify() | printStackTrace() |
wait() | printStackTrace(PrintWriter) |
wait(long, int) | getLocalizedMessage() |
wait(long) | printStackTrace(PrintStream) |
equals(Object) | |
toString() | |
finalize() | |
hashCode() | |
notifyAll() |
java.lang.Errors class provides several errors that can be thrown from java.lang package under several circumstances. The popular error classes are as follows:
Error class | Description |
ThreadDeath | It is thrown in a victim thread where the stop() method with zero arguments is called. |
UnsatisfiedLinkError | This error is thrown when JVM does not find the native-language definition of a particular method. |
AbstractMethodError | This error is thrown when an abstract method is called from an application. |
AssertionError | This error is thrown from ana application when an assertion fails. |
ClassCircularityError | This error is thrown when JVM detects a circularity when it initializes the class. |
ClassFormatError | This error is thrown when JVM tries to read a class file that is malformed or tampered or JVM can not identify this file as a valid class file. |
Error | Child class of throwable. It indicates that a serious issue has occurred during runtime. These issues are not created to catch operation to handle. |
ExceptionInInitializerError | This error is thrown when JVM detects a serious issue in the static initialization process. |
IllegalAccessError | This error is thrown when an application tries to access, edit or modify a filed or tries to call a method and the application does not have access to those. |
IncompatibleClassChangeError | This error is thrown when the class definition is changed incompatibly and the JVM tries to access the same. |
InstantiationError | This error is thrown when the application calls a new operator to instantiate an interface or an abstract class. |
InternalError | This error is thrown when JVM detects some internal error which is unexpected but unrecoverable. |
LinkageError | This error occurs when the linking subclass has some dependency on another class. |
NoClassDefFoundError | This error occurs when JVM(classloader) tries to load a class and the definition of the class is not found. |
NoSuchFieldError | This error occurs when the application tries to access, edit, modify a field of a given object but the object has not the field present. |
NoSuchMethodError | This error occurs when the application tries to access, edit, modify a method of a given object but the object has not the method present. |
OutOfMemoryError | This error occurs when garbage collector can not provide enough space as a result the JVN can not provide memory to an object. |
StackOverflowError | This error occurs when the application recurses too deeply and the stack is full. |
UnknownError | A serious error whose origination is unknown but thrown by JVM. |
UnsupportedClassVersionError | This error occurs when the JVM detects the major and minor version is mismatching while reading a class file. |
VerifyError | This error occurs when JVM (verifier) detects a well-formed class with some potential security violation or internal inconsistency. |
VirtualMachineError | This error occurs when JVM runs out of resources or kind of broken and can not continue its operations. |
AWTError | Thrown from an application when serious AWT error occurred during runtime. |