Skip to content

Javascript's Error is not a Throwable unless java.lang.Error class was loaded first. #239

Description

@warownia1

Javascript's native Error objects are not instances of java.lang.Throwable which can be verified with the following code

Throwable error = null;
/** @j2sNative error = new Clazz.Error(); */
assert !(error instanceof Throwable);  // pass

However, loading java.lang.Error class before instanceof Throwable is invoked for the first time causes consecutive instance checks to be true

var _ignored = Error.class;
Throwable error = null;
/** @j2sNative error = new Clazz.Error(); */
assert !(error instanceof Throwable);  // fail

Additionally, if the instance test was performed at least once, loading the java.lang.Error class has no effect

Throwable error = null;
/** @j2sNative error = new Clazz.Error(); */
assert !(error instanceof Throwable);  // pass
var _ignored = Error.class;
assert !(error instanceof Throwable);  // also pass

The relation is non-transitive for subclasses of Error i.e. TypeError is Error and Error is Throwable, but TypeError is not Throwable

Throwable error = null, typeError = null;
/** @j2sNative
 * error = new Clazz.Error();
 * typeError = new TypeError(); */
var _ignored = Error.class;
assert error instanceof Throwable;  // pass
assert typeError instanceof Throwable; // fail

Even though an Error is a Throwable, it doesn't declare its methods

var _ignored = Error.class;
Throwable error = null;
/** @j2sNative error = new Clazz.Error(); */
if (error instanceof Throwable)
  error.getMessage();  // TypeError: error.getMessage$ is not a function

Expectations:

  • an Error should consistently be, or not be a Throwable
  • if an Error is a Throwable, then all its subclasses also should be and vice versa
  • if an Error is a Throwable, then it should also have all the methods declared by the Throwable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions