Java : Error with Examples
Error (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Error methods.
Summary
try {
final var array = new int[1000000000];
} catch (Error e) {
System.out.println(e);
}
// Result
// ↓
//java.lang.OutOfMemoryError: Java heap space
Constructors
Error ()
final var e = new Error();
System.out.println(e); // java.lang.Error
Error (String 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)
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)
@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)
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
- API Examples
- Error
- Exception
- RuntimeException
- ArithmeticException
- ArrayIndexOutOfBoundsException
- ArrayStoreException
- CancellationException
- ClassCastException
- ConcurrentModificationException
- DateTimeException
- DateTimeParseException
- IllegalArgumentException
- IllegalStateException
- IndexOutOfBoundsException
- NoSuchElementException
- NullPointerException
- PatternSyntaxException
- StringIndexOutOfBoundsException
- UnsupportedOperationException
- Throwable