Java : Error with Examples

Error (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Error methods.


Summary

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.

Class diagram

try {
    final var array = new int[1000000000];
} catch (Error e) {
    System.out.println(e);
}

// Result
// ↓
//java.lang.OutOfMemoryError: Java heap space

Constructors

Error ()

Constructs a new error with null as its detail message.

final var e = new Error();
System.out.println(e); // java.lang.Error

Error (String message)

Constructs a new error with the specified detail message.

final var e = new Error("abcd");
System.out.println(e); // java.lang.Error: abcd
System.out.println(e.getMessage()); // abcd

Error (String message, Throwable cause)

Constructs a new error with the specified detail message and cause.

final var cause = new Error("XYZ");
final var e = new Error("abcd", cause);

System.out.println(e); // java.lang.Error: abcd
System.out.println(e.getMessage()); // abcd

System.out.println(e.getCause()); // java.lang.Error: XYZ
System.out.println(e.getCause().getMessage()); // XYZ

Error (String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)

Constructs a new error with the specified detail message, cause, suppression enabled or disabled, and writable stack trace enabled or disabled.

@SuppressWarnings("serial")
class SampleError extends Error {
    SampleError(String message, Throwable cause,
                boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
final var cause = new Error("XYZ");
final var e = new SampleError("abcd", cause, true, true);

System.out.println(e); // SampleError: abcd
System.out.println(e.getCause()); // java.lang.Error: XYZ

e.addSuppressed(new Error("E1"));
e.addSuppressed(new Error("E2"));

// [java.lang.Error: E1, java.lang.Error: E2]
System.out.println(Arrays.toString(e.getSuppressed()));

System.out.println(e.getStackTrace().length > 0); // true
// enableSuppression = false
final var cause = new Error("XYZ");
final var e = new SampleError("abcd", cause, false, true);

System.out.println(e); // SampleError: abcd
System.out.println(e.getCause()); // java.lang.Error: XYZ

e.addSuppressed(new Error("E1"));
e.addSuppressed(new Error("E2"));
System.out.println(Arrays.toString(e.getSuppressed())); // []

System.out.println(e.getStackTrace().length > 0); // true
// writableStackTrace = false
final var cause = new Error("XYZ");
final var e = new SampleError("abcd", cause, true, false);

System.out.println(e); // SampleError: abcd
System.out.println(e.getCause()); // java.lang.Error: XYZ

e.addSuppressed(new Error("E1"));
e.addSuppressed(new Error("E2"));

// [java.lang.Error: E1, java.lang.Error: E2]
System.out.println(Arrays.toString(e.getSuppressed()));

System.out.println(e.getStackTrace().length); // 0

Error (Throwable cause)

Constructs a new error with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).

final var cause = new Error("XYZ");
final var e = new Error(cause);

System.out.println(e); // java.lang.Error: java.lang.Error: XYZ
System.out.println(e.getMessage()); // java.lang.Error: XYZ

System.out.println(e.getCause()); // java.lang.Error: XYZ
System.out.println(e.getCause().getMessage()); // XYZ

Methods declared in Throwable

addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString

Please see the link below.


Related posts

To top of page