Common Error classes in Java

0
736
Spread the love
  • 23
  • 34
  • 58
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    162
    Shares

NoSuchMethodError in Java

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.

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.
}

NoSuchMethodError is thrown from an application if the application tries to call a static or instance method of another class and that class no longer has a method defined.So this error signals that the specified method could not be found.
It is a child class of IncompatibleClassChangeError which is a sub class of LinkageError.

The structure of the NoSuchMethodError class is given below:

public class java.lang.NoSuchMethodError extends java.lang.IncompatibleClassChangeError{
//constructor
public NoSuchMethodError();
//constructs an empty NoSuchMethodError object that is an object with no message specified.
public NoSuchMethodError(String message);
//constructs an NoSuchMethodError object with the message specified.
}

Class NoSuchFieldError in Java

NoSuchFieldError is thrown from an application if an application tries to access or modify a specified field of an object, and that object no longer has the field. In general, this error is caught during compilation time but can come in run time if the definition of the class has incompatibly changed.

In a nutshell, NoSuchFieldError signals that a specified field could not be found.

The structure of the class NoSuchFieldError  is given as:

public class java.lang.NoSuchFieldError extends java.lang.IncompatibleClassChangeError{
//constructor
public NoSuchFieldError();
//constructs an empty NoSuchFieldError object that is an object with no message specified.
public NoSuchFieldError(String message);
//constructs an NoSuchFieldError object with the message specified.
}

LinkageError in Java:

LinkageError is thrown from an application when a class has some dependency on another class but that class is not link able. This is also the superclass of a group of errors that indicates linking problem or resolving dependencies among classes.

The structure of the LinkageError class is defined as below:

public class java.lang.LinkageError extends java.lang.Error{
//constructor
public LinkageError();
//constructs an empty LinkageError object that is an object with no message specified.
public LinkageError(String message);
//constructs an LinkageError object with the message specified.
}

Class InternalError in Java

InternalError is thrown from an application during run time to indicate that some unexpected internal error occurred in the java virtual machine. This is very rare.

The structure of the class InternalError is given as:

public class java.lang.InternalError extends java.lang.VirtualMachineError{
//constructor
public InternalError();
//constructs an empty InternalError object that is an object with no message specified.
public InternalError(String message);
//constructs an InternalError object with the message specified.
}

NoClassDefFoundError in Java:

NoClassDefFoundError is thrown from an application if the Java virtual machine or a class loader tries to load the definition of a class(as part of the normal method call or as part of creating a new instance using new operator) and no definition could be found in the path.

The structure of the class NoClassDefFoundError  is given as:

public class java.lang.NoClassDefFoundError extends java.lang.LinkageError{
//constructor
public NoClassDefFoundError();
//constructs an empty NoClassDefFoundError object that is an object with 
//no message specified.
public NoClassDefFoundError(String message);
//constructs an NoClassDefFoundError object with the message specified.
}

InstantiationError in Java

InstantiationError is thrown from an application when an application tries to use the java new operator to instantiate an abstract class or an interface. Normally this error is caught by the compiler. This error can only occur at run time if the definition of the class has incompatibly changed.

The structure of the class InstantiationError is given below:

public class java.lang.InstantiationError extends java.lang.IncompatibleClassChangeError{
//constructor
public InstantiationError();
//constructs an empty InstantiationError object that is an object with no message specified.
public InstantiationError(String message);
//constructs an InstantiationError object with the message specified.
}

Class IncompatibleClassChangeError in Java

IncompatibleClassChangeError is thrown from an application to indicate that an incompatible class change has occurred to some class definition. The definition of some class on which the currently executing method depends has changed.

The structure of the IncompatibleClassChangeError class is given below:

public class java.lang.IncompatibleClassChangeError extends java.lang.LinkageError{
//constructor
public IncompatibleClassChangeError();
//constructs an empty IncompatibleClassChangeError object that is an object with no message specified.
public IncompatibleClassChangeError(String message);
//constructs an IncompatibleClassChangeError object with the message specified.
}

Class IllegalAccessError in Java

IllegalAccessError is thrown from an application if an application attempts to access or modify a field or calla a method for which it does not have access to. In general this error is caught by the compiler. In very rare case during the run time, if the class definition is changed incompatibly we may get this error.

The structure of the IllegalAccessError class given as:

public class java.lang.IllegalAccessError extends java.lang.IncompatibleClassChangeError{
//constructor
public IllegalAccessError();
//constructs an empty IllegalAccessError object that is an object with no message specified.
public IllegalAccessError(String message);
//constructs an IllegalAccessError object with the message specified.
}

Class ClassFormatError in Java

ClassFormatError is thrown from an application when java virtual machine attempts to read a class file and determines that the file is malformed or can not be interpreted as a valid class file.

The structure of the ClassFormatError class is given as:

public class java.lang.ClassFormatError extends java.lang.LinkageError{
//constructor
public ClassFormatError();
//constructs an empty ClassFormatError object that is an object with no message specified.
public ClassFormatError(String message);
//constructs an ClassFormatError object with the message specified.
}

Class ClassCircularityError in Java

ClassCircularityError is thrown from a program when a class extends to the same class or an interface extends to same interface. This is a very rare scenario but can happen. The major reason to get this error is to have multiple version of the same class present in the package structure(library). When JVM load the class it can detect the circularity and throws the error.

The structure of ClassCircularityError is given below:

public class java.lang.ClassCircularityError extends java.lang.LinkageError{
//constructor
public ClassCircularityError();
//constructs an empty ClassCircularityError object that is an object with no message specified.
public ClassCircularityError(String message);
//constructs an ClassCircularityError object with the message specified.
}

AbstractMethodError in Java

AbstractMethodError is thrown when an application tries to call an abstract method.Normally this error is caught by the compiler.This error can only occur at run time if the definition of some class has incompatibly changed since the currently executing method was last compiled.

The structure of AbstractMethodError is defined as below:

public class java.lang.AbstractMethodError extends
             java.lang.IncompatibleClassChangeError{
//constructor
public AbstractMethodError();
//constructs an empty AbstractMethodError object that is an object with no 
//message specified.
public AbstractMethodError(String message);
//constructs an AbstractMethodError object with the message specified.
}

Apart from these this class also has the below inherited methods:
From Object class”

  • clone()
  • getClass()
  • equals(Object obj);
  • finalize()
  • hashCode()
  • notify()
  • notifyAll()
  • wait()
  • wait(long timeput)
  • wait(long timeout,int nanos)
  • toString()

From Throwable:

  • fillInStackTrace()
  • getMessage()
  • getLocalizedMessage()
  • printStackTrace()
  • printStackTrace(PrintWriter writer)
  • printStackTrace(PrintStream out)

Class UnsatisfiedLinkError in Java

UnsatisfiedLinkError is thrown from an application if the java virtual machine can not find an appropriate native language definition of a method but declared as native.

The structure of the Class UnsatisfiedLinkError is given as:

public class java.lang.UnsatisfiedLinkError extends java.lang.LinkageError{
//constructor
public UnsatisfiedLinkError();
//constructs an empty UnsatisfiedLinkError object that is an object with no message specified.
public UnsatisfiedLinkError(String message);
//constructs an UnsatisfiedLinkError object with the message specified.
}

Class UnknownError in Java

UnknownError is thrown from an application when some serious error occurs in the Java virtual machine and JVM can not categorize it.

The structure of the class UnknownError  is given below:

public class java.lang.UnknownError extends java.lang.VirtualMachineError{
//constructor
public UnknownError();
//constructs an empty UnknownError object that is an object with no message specified.
public UnknownError(String message);
//constructs an UnknownError object with the message specified.
}

ThreadDeath Error in Java:

ThreadDeath error instance is thrown from an application when the stop() method with zero arguments is called for a thread. The victim thread throws this error to indicate that it has been killed without providing any reason. It also indicates that the victim thread should terminate.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 23
  • 34
  • 58
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    162
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here