Class UnknownError in Java

Class UnknownError in Java

UnknownError is thrown from an application when some serious error occurs from an unknown origin in the Java virtual machine and JVM can not categorize it. This error also does not match to any documented conditions in JVM.

This is a very rare error.

Java Namespace: java.lang. The same error is also available in Android in Mono.Android.dll

The class hierarchy of UnknownError in Java


class hierarchy of UnknownError in Java
the class hierarchy of UnknownError in Java

UnknownError in Java implements Serializable interface.

Note: Java program may compile fine but fails during execution and there is no point to catch this error using a try catch block.

The structure of the class UnknownError  is given below:

public class java.lang.UnknownError extends java.lang.VirtualMachineError{
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.

The class UnknownError also inherits methods from class Object and Throwable.

From Object classFrom Throwable class
wait(long, int)getLocalizedMessage()

Fields inherited from class java.lang.Throwable

backtrace, detailMessage, serialVersionUID

How to create UnknownError?

Below is the few codes snippet to generate the UnknownError:

private void someMethod() {
     UnknownError u = ocationTargetException 
invocationTargetException;new UnknownError(invocationTargetException.getMessage())

Using NewInstance.getClassLoader()

 static ClassLoader getClassLoader ()
    Method m = null;

    try {
      m = Thread.class.getMethod("getContextClassLoader");
    } catch (NoSuchMethodException e) {
      return NewInstance.class.getClassLoader();

    try {
      return (ClassLoader) m.invoke(Thread.currentThread());
    } catch (IllegalAccessException e) {
      // assert(false)
      throw new UnknownError(e.getMessage());
    } catch (InvocationTargetException e) {
      // assert(e.getTargetException() instanceof SecurityException)
      throw new UnknownError(e.getMessage());

Using ThrowablesTest.testRethrowError()

@Test(expected = UnknownError.class)
public void testRethrowError() throws Exception {
  Throwables.rethrow(new UnknownError());

UnknownError in Android Context


See also  Class MuiticastSocket in Java


UnknownError()Creates a new UnknownError which includes the current stack trace.
UnknownError(IntPtr, JniHandleOwnership)A constructor used when creating managed representations of JNI objects; It is called by the runtime.
UnknownError(String)Creates a new UnknownError with the current stack trace and the specified detail message.



CauseReturns the cause of this Throwable, or null if there is no cause.

(Inherited from Throwable)

ClassInherited from Throwable
HandleThe handle to the underlying Android instance.

(Inherited from Throwable)

IJavaObjectEx.IsProxyInherited from Throwable
IJavaObjectEx.KeyHandleInherited from Throwable
IJavaObjectEx.NeedsActivationInherited from Throwable
IJavaPeerable.JniManagedPeerStateInherited from Throwable
JniIdentityHashCodeInherited from Throwable
LocalizedMessageReturns the detail message which was provided when this Throwable was created. (Inherited from Throwable)
MessageReturns the detail message which was provided when this Throwable was created. (Inherited from Throwable)
PeerReferenceInherited from Throwable
StackTraceInherited from Throwable
ThresholdClassThis API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
ThresholdTypeThis API supports the Mono for Android infrastructure and is not intended to be used directly from your code.


AddSuppressed(Throwable)Adds throwable to the list of throwables suppressed by this. (Inherited from Throwable)
Dispose()Inherited from Throwable
Dispose(Boolean)Inherited from Throwable
FillInStackTrace()Records the stack trace from the point where this method has been called to this Throwable. (Inherited from Throwable)
GetStackTrace()Returns a clone of the array of stack trace elements of this Throwable.

(Inherited from Throwable)

GetSuppressed()Returns the throwables suppressed by this. (Inherited from Throwable)
InitCause(Throwable)Initializes the cause of this Throwable. (Inherited from Throwable)
PrintStackTrace()Writes a printable representation of this Throwable’s stack trace to the System.err stream. (Inherited from Throwable)
PrintStackTrace(PrintStream)Writes a printable representation of this Throwable’s stack trace to the given print stream. (Inherited from Throwable)
PrintStackTrace(PrintWriter)Writes a printable representation of this Throwable’s stack trace to the specified print writer. (Inherited from Throwable)
SetHandle(IntPtr, JniHandleOwnership)Sets the Handle property. (Inherited from Throwable)
SetStackTrace(StackTraceElement[])Sets the array of stack trace elements. (Inherited from Throwable)
ToString()Inherited from Throwable
UnregisterFromRuntime()Inherited from Throwable


is_generatedInherited from Throwable

Explicit Interface Implementations

IJavaObjectEx.ToLocalJniHandle()Inherited from Throwable
IJavaPeerable.Disposed()Inherited from Throwable
IJavaPeerable.DisposeUnlessReferenced()Inherited from Throwable
IJavaPeerable.Finalized()Inherited from Throwable
IJavaPeerable.SetJniIdentityHashCode(Int32)Inherited from Throwable
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates)Inherited from Throwable
IJavaPeerable.SetPeerReference(JniObjectReference)Inherited from Throwable


Extension Methods

JavaCast<TResult>(IJavaObject)Performs an Android runtime-checked type conversion.

Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *