Exception in initializer error java



How to Handle the Exception In Initializer Runtime Error in Java

Table of Contents

Introduction to Runtime Errors & Exceptions

Unlike compile-time errors which are detected during compilation [1], runtime errors occur during program execution, i.e. runtime. Java’s runtime error hierarchy is somewhat complicated compared to other programming languages, but at the basic level there are two main categories: runtime errors and runtime exceptions, the latter of which being further divided into checked and unchecked exceptions (see Figure 1 below). Unchecked exceptions are also lumped into the somewhat confusingly named RuntimeException superclass, while all runtime errors are also considered to be unchecked. The term “unchecked” refers to errors and exceptions that Java doesn’t require to be caught or otherwise specified in the code [2]. Runtime Java errors and exceptions are otherwise jointly referred to as throwables, as per the name of the Throwable class—the parent class of all errors and exceptions in this language [3].

Figure 1. Java runtime errors & exceptions hierarchy [4]

ExceptionInInitializerError Error: What, Why & How?

After successfully compiling a program, the Java Virtual Machine (JVM) performs dynamic loading, linking, and initializing of classes and interfaces, broadly known as the class loading process [5]. This process includes the evaluation of all static initializer blocks and variable assignments present in the compiled code. If, during this evaluation, any unexpected exception occurs, the JVM throws an ExceptionInInitializerError runtime error, points to the specific exception that caused the error, and subsequently exits the program.

The ExceptionInInitializerError error occurs every time there is an unchecked (and uncaught) exception taking place inside a static initializer or a static variable assignment. The JVM wraps this exception inside an instance of the java.lang.ExceptionInInitializerError class (which itself is a subclass of the more generic java.lang.LinkageError class of errors [6]) and maintains a reference to it as the root cause.

How to handle the ExceptionInInitializerError Error

To avoid this error, simply ensure that:

  • static initializers of classes do not throw any unchecked exception, and that
  • static class variable initializations do not throw any unchecked exceptions.

Источник

java.lang.exceptionininitializererror – How to handle Exception Initializer Error

In this tutorial we will discuss about Java’s ExceptionInInitializerError and how to deal with it. The ExceptionInInitializerError is a sub-class of the LinkageError class and denotes that an unexpected exception has occurred in a static initializer or the initializer for a static variable. As of Java release 1.4, this error conforms to the general purpose exception-chaining mechanism.

Читайте также:  Db4 error run database recovery

The ExceptionInInitializerError is thrown when the JVM attempts to load a new class. During the class loading procedure, all static variables and static initializers are being evaluated. A static initializer is a block enclosed within curly braces without having any name and return type, except having the keyword static .

A sample example of a static initializer is shown below:

The static initializer is evaluated only once during the class loading procedure. Thus, a thrown exception in the evaluation of a static variable or initializer is wrapped into an ExceptionInInitializerError , in order for the JVM to indicate that the class could not be initialized and loaded.

A sample example that throws the aforementioned error is the following:

If we execute the above code snippet, we will receive the following error:

We can use the following methods, in order to retrieve more information about the underlying exception:

  • getException() : Returns the exception that occurred during a static initialization that caused this error to be created. However, after the exception has conformed to the general-purpose exception chaining, the preferred method to use is getCause .
  • getCause() :Returns the exception that caused this error to be thrown.

How to deal with the ExceptionInInitializerError

The ExceptionInInitializerError is used as a wrapper to indicate that an exception arises in the static initializer block or the evaluation of a static variable’s value. Thus, we have to ensure that the original exception is fixed, in order for the JVM to be able to load our class successfully.

Final comments on the Error

It is very important to mention that you can throw unchecked / runtime exceptions from the block of a static initializer. However, you cannot allow a checked exception to propagate out of a static block, because is not possible to handle these exceptions in your source.

Источник

Exception in initializer error java

Use is subject to License Terms. Your use of this web site or any of its content or software indicates your agreement to be bound by these License Terms.

Copyright © 2006 Sun Microsystems, Inc. All rights reserved.

Overview Package Class Use Tree Deprecated Index Help
CDC 1.1.2
PREV CLASS NEXT CLASS FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD

java.lang
Class ExceptionInInitializerError

Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

Читайте также:  Runtime error 216 at 00403b8a космические рейнджеры 2

As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The «saved throwable object» that may be provided at construction time and accessed via the getException() method is now known as the cause, and may be accessed via the Throwable.getCause() method, as well as the aforementioned «legacy method.»

Constructor Summary
ExceptionInInitializerError ()
Constructs an ExceptionInInitializerError with null as its detail message string and with no saved throwable object.
ExceptionInInitializerError (String s)
Constructs an ExceptionInInitializerError with the specified detail message string.
ExceptionInInitializerError (Throwable thrown)
Constructs a new ExceptionInInitializerError class by saving a reference to the Throwable object thrown for later retrieval by the getException() method.
Method Summary
Throwable getCause ()
Returns the cause of this error (the exception that occurred during a static initialization that caused this error to be created).
Throwable getException ()
Returns the exception that occurred during a static initialization that caused this error to be created.
Methods inherited from class java.lang.Throwable
fillInStackTrace, getLocalizedMessage, getMessage, getStackTrace, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructor Detail

ExceptionInInitializerError


ExceptionInInitializerError

Parameters: thrown — The exception thrown

ExceptionInInitializerError

Parameters: s — the detail message

Method Detail

getException

This method predates the general-purpose exception chaining facility. The Throwable.getCause() method is now the preferred means of obtaining this information.

Returns: the saved throwable object of this ExceptionInInitializerError , or null if this ExceptionInInitializerError has no saved throwable object.

getCause

Overrides: getCause in class Throwable Returns: the cause of this error or null if the cause is nonexistent or unknown. Since: 1.4

Overview Package Class Use Tree Deprecated Index Help
CDC 1.1.2
PREV CLASS NEXT CLASS FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD
Copyright © 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to License Terms. Your use of this web site or any of its content or software indicates your agreement to be bound by these License Terms.

For more information, please consult the JSR 218 specification.

Источник

Exception in initializer error java

Use is subject to License Terms. Your use of this web site or any of its content or software indicates your agreement to be bound by these License Terms.

Copyright © 2006 Sun Microsystems, Inc. All rights reserved.

Overview Package Class Use Tree Deprecated Index Help
JSR 216 (Maintenance Release)
PREV CLASS NEXT CLASS FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD
Читайте также:  Error detection encoder decoder

java.lang
Class ExceptionInInitializerError

Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The «saved throwable object» that may be provided at construction time and accessed via the getException() method is now known as the cause, and may be accessed via the Throwable.getCause() method, as well as the aforementioned «legacy method.»

Constructor Summary
ExceptionInInitializerError ()
Constructs an ExceptionInInitializerError with null as its detail message string and with no saved throwable object.
ExceptionInInitializerError (String s)
Constructs an ExceptionInInitializerError with the specified detail message string.
ExceptionInInitializerError (Throwable thrown)
Constructs a new ExceptionInInitializerError class by saving a reference to the Throwable object thrown for later retrieval by the getException() method.
Method Summary
Throwable getCause ()
Returns the cause of this error (the exception that occurred during a static initialization that caused this error to be created).
Throwable getException ()
Returns the exception that occurred during a static initialization that caused this error to be created.
Methods inherited from class java.lang.Throwable
fillInStackTrace, getLocalizedMessage, getMessage, getStackTrace, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructor Detail

ExceptionInInitializerError


ExceptionInInitializerError

Parameters: thrown — The exception thrown

ExceptionInInitializerError

Parameters: s — the detail message

Method Detail

getException

This method predates the general-purpose exception chaining facility. The Throwable.getCause() method is now the preferred means of obtaining this information.

Returns: the saved throwable object of this ExceptionInInitializerError , or null if this ExceptionInInitializerError has no saved throwable object.

getCause

Overrides: getCause in class Throwable Returns: the cause of this error or null if the cause is nonexistent or unknown. Since: 1.4

Overview Package Class Use Tree Deprecated Index Help
JSR 216 (Maintenance Release)
PREV CLASS NEXT CLASS FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD
Copyright © 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to License Terms. Your use of this web site or any of its content or software indicates your agreement to be bound by these License Terms.

For more information, please consult the JSR 216 specification.

Источник

Оцените статью
toolgir.ru
Adblock
detector