Java : Throwable 示例
Throwable (Java SE 22 & JDK 22) 示例。
您将在大多数 Throwable 方法中找到代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
Throwable 类是 Java 语言中所有错误和异常的超类。只有此类(或其子类之一)的实例对象才会被 Java 虚拟机抛出,或者可以通过 Java throw 语句抛出。 (机器翻译)
Code examples on this page uses the SampleException class below.
@SuppressWarnings("serial")
public class SampleException extends Exception {
public SampleException() {
}
public SampleException(String message) {
super(message);
}
public SampleException(String message, Throwable cause) {
super(message, cause);
}
public SampleException(Throwable cause) {
super(cause);
}
public SampleException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
public class Main {
public static void main(String[] args) {
try {
func1();
} catch (SampleException e) {
final Throwable t = e;
t.printStackTrace();
// Result
// ↓
// SampleException
// Main.func2 ...
// Main.func1 ...
// Main.main ...
// ...
}
}
private static void func1() throws SampleException {
func2();
}
private static void func2() throws SampleException {
throw new SampleException();
}
}
Constructors
Throwable ()
构造一个新的可抛出对象,以 null 作为其详细消息。 (机器翻译)
final Throwable t = new SampleException();
System.out.println(t); // SampleException
Throwable (String message)
使用指定详细消息构造一个新的可抛出对象。 (机器翻译)
final Throwable t = new SampleException("abcde");
System.out.println(t); // SampleException: abcde
System.out.println(t.getMessage()); // abcde
Throwable (String message, Throwable cause)
使用指定的详细消息和原因构造一个新的可抛出对象。 (机器翻译)
final Throwable cause = new SampleException("XYZ");
final Throwable t = new SampleException("abcde", cause);
System.out.println(t); // SampleException: abcde
System.out.println(t.getMessage()); // abcde
System.out.println(t.getCause()); // SampleException: XYZ
System.out.println(t.getCause().getMessage()); // XYZ
Throwable (String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)
构造一个新的可抛出对象,带有指定详细消息、原因、启用或禁用抑制以及启用或禁用可写堆栈跟踪。 (机器翻译)
final Throwable cause = new SampleException("XYZ");
final Throwable t = new SampleException("abcde", cause, true, true);
System.out.println(t); // SampleException: abcde
System.out.println(t.getCause()); // SampleException: XYZ
t.addSuppressed(new SampleException("E1"));
t.addSuppressed(new SampleException("E2"));
// [SampleException: E1, SampleException: E2]
System.out.println(Arrays.toString(t.getSuppressed()));
System.out.println(t.getStackTrace().length > 0); // true
// enableSuppression = false
final Throwable cause = new SampleException("XYZ");
final Throwable t = new SampleException("abcde", cause, false, true);
System.out.println(t); // SampleException: abcde
System.out.println(t.getCause()); // SampleException: XYZ
t.addSuppressed(new SampleException("E1"));
t.addSuppressed(new SampleException("E2"));
System.out.println(Arrays.toString(t.getSuppressed())); // []
System.out.println(t.getStackTrace().length > 0); // true
// writableStackTrace = false
final Throwable cause = new SampleException("XYZ");
final Throwable t = new SampleException("abcde", cause, true, false);
System.out.println(t); // SampleException: abcde
System.out.println(t.getCause()); // SampleException: XYZ
t.addSuppressed(new SampleException("E1"));
t.addSuppressed(new SampleException("E2"));
// [SampleException: E1, SampleException: E2]
System.out.println(Arrays.toString(t.getSuppressed()));
System.out.println(t.getStackTrace().length); // 0
Throwable (Throwable cause)
使用指定的原因和详细消息 (cause==null ? null : cause.toString()) 构造一个新的可抛出对象(通常包含原因的类和详细消息)。 (机器翻译)
final Throwable cause = new SampleException("XYZ");
final Throwable t = new SampleException(cause);
System.out.println(t); // SampleException: SampleException: XYZ
System.out.println(t.getMessage()); // SampleException: XYZ
System.out.println(t.getCause()); // SampleException: XYZ
System.out.println(t.getCause().getMessage()); // XYZ
Methods
final void addSuppressed (Throwable exception)
将指定的异常附加到被抑制的异常中,以便传递此异常。 (机器翻译)
final Throwable t = new SampleException("abcd");
System.out.println(t); // SampleException: abcd
t.addSuppressed(new SampleException("E1"));
t.addSuppressed(new SampleException("E2"));
final var ret = t.getSuppressed();
System.out.println(Arrays.toString(ret)); // [SampleException: E1, SampleException: E2]
Throwable fillInStackTrace ()
填写执行堆栈跟踪。 (机器翻译)
public class Main {
public static void main(String[] args) {
try {
func1();
} catch (SampleException e) {
final Throwable t = e;
System.out.println("-- getStackTrace --");
for (final var element : t.getStackTrace()) {
System.out.println(element);
}
// Result
// ↓
//-- getStackTrace --
// Main.func2 ...
// Main.func1 ...
// Main.main ...
// ...
// The stack trace is overwritten.
final var ret = t.fillInStackTrace();
System.out.println(ret); // SampleException
System.out.println("-- getStackTrace --");
for (final var element : t.getStackTrace()) {
System.out.println(element);
}
// Result
// ↓
//-- getStackTrace --
// Main.main ...
// ...
}
}
private static void func1() throws SampleException {
func2();
}
private static void func2() throws SampleException {
throw new SampleException();
}
}
Throwable getCause ()
返回此可抛出事件的原因,如果原因不存在或未知,则返回 null。 (机器翻译)
final Throwable t = new SampleException();
System.out.println(t); // SampleException
System.out.println(t.getCause()); // null
final Throwable cause = new SampleException("XYZ");
final Throwable t = new SampleException(cause);
System.out.println(t); // SampleException: SampleException: XYZ
System.out.println(t.getCause()); // SampleException: XYZ
System.out.println(t.getCause().getMessage()); // XYZ
String getLocalizedMessage ()
创建此可抛出对象的本地化描述。 (机器翻译)
@SuppressWarnings("serial")
class LocalizedException extends Exception {
private final static String MESSAGE_EN = "Exception! (EN)";
private final static String MESSAGE_JA = "Exception! (JA)";
LocalizedException() {
super(MESSAGE_EN);
}
@Override
public String getLocalizedMessage() {
// It is a simple implementation.
final var locale = Locale.getDefault();
return switch (locale.getLanguage()) {
case "ja" -> MESSAGE_JA;
default -> MESSAGE_EN;
};
}
}
{
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final Throwable t = new LocalizedException();
System.out.println(t); // LocalizedException: Exception! (EN)
System.out.println(t.getMessage()); // Exception! (EN)
System.out.println(t.getLocalizedMessage()); // Exception! (EN)
}
{
Locale.setDefault(Locale.JAPAN);
System.out.println(Locale.getDefault().toLanguageTag()); // ja-JP
final Throwable t = new LocalizedException();
System.out.println(t); // LocalizedException: Exception! (JA)
System.out.println(t.getMessage()); // Exception! (EN)
System.out.println(t.getLocalizedMessage()); // Exception! (JA)
}
String getMessage ()
返回此可抛出对象的详细消息字符串。 (机器翻译)
final Throwable t = new SampleException("abcd");
System.out.println(t); // SampleException: abcd
System.out.println(t.getMessage()); // abcd
StackTraceElement[] getStackTrace ()
提供对 printStackTrace() 打印的堆栈跟踪信息的编程访问。 (机器翻译)
public class Main {
public static void main(String[] args) {
try {
func1();
} catch (SampleException e) {
final Throwable t = e;
System.out.println("-- getStackTrace --");
for (final var element : t.getStackTrace()) {
System.out.println(element);
}
// Result
// ↓
//-- getStackTrace --
// Main.func2 ...
// Main.func1 ...
// Main.main ...
// ...
}
}
private static void func1() throws SampleException {
func2();
}
private static void func2() throws SampleException {
throw new SampleException();
}
}
final Throwable[] getSuppressed ()
返回一个数组,其中包含所有被抑制的异常(通常通过 try-with-resources 语句),以便传递此异常。 (机器翻译)
final Throwable t = new SampleException("abcd");
System.out.println(t); // SampleException: abcd
t.addSuppressed(new SampleException("E1"));
t.addSuppressed(new SampleException("E2"));
final var ret = t.getSuppressed();
System.out.println(Arrays.toString(ret)); // [SampleException: E1, SampleException: E2]
Throwable initCause (Throwable cause)
将此可抛出事件的原因初始化为指定值。 (机器翻译)
final Throwable t = new SampleException();
System.out.println(t); // SampleException
System.out.println(t.getCause()); // null
final Throwable cause = new SampleException("XYZ");
System.out.println(t.initCause(cause)); // SampleException
System.out.println(t.getCause()); // SampleException: XYZ
void printStackTrace ()
将此可抛出对象及其回溯打印到标准错误流。 (机器翻译)
public class Main {
public static void main(String[] args) {
try {
func1();
} catch (SampleException e) {
final Throwable t = e;
t.printStackTrace();
// Result
// ↓
// SampleException
// Main.func2 ...
// Main.func1 ...
// Main.main ...
// ...
}
}
private static void func1() throws SampleException {
func2();
}
private static void func2() throws SampleException {
throw new SampleException();
}
}
void printStackTrace (PrintStream s)
将此可抛出对象及其回溯打印至指定的打印流。 (机器翻译)
public class Main {
public static void main(String[] args) throws IOException {
try {
func1();
} catch (SampleException e) {
final Throwable t = e;
final var path = Path.of("R:", "java-work", "aaa.txt");
try (final var s = new PrintStream(Files.newOutputStream(path))) {
t.printStackTrace(s);
}
System.out.println("-- readString --");
System.out.println(Files.readString(path));
// Result
// ↓
//-- readString --
// SampleException
// Main.func2 ...
// Main.func1 ...
// Main.main ...
// ...
}
}
private static void func1() throws SampleException {
func2();
}
private static void func2() throws SampleException {
throw new SampleException();
}
}
void printStackTrace (PrintWriter s)
将此可抛出对象及其回溯打印到指定的打印编写器。 (机器翻译)
public class Main {
public static void main(String[] args) throws IOException {
try {
func1();
} catch (SampleException e) {
final Throwable t = e;
final var stringWriter = new StringWriter();
try (final var printWriter = new PrintWriter(stringWriter)) {
t.printStackTrace(printWriter);
}
System.out.println("-- StringWriter --");
System.out.println(stringWriter);
// Result
// ↓
//-- StringWriter --
// SampleException
// Main.func2 ...
// Main.func1 ...
// Main.main ...
// ...
}
}
private static void func1() throws SampleException {
func2();
}
private static void func2() throws SampleException {
throw new SampleException();
}
}
void setStackTrace (StackTraceElement[] stackTrace)
设置将由 getStackTrace() 返回并由 printStackTrace() 和相关方法打印的堆栈跟踪元素。 (机器翻译)
final Throwable t = new SampleException();
final var elements = List.of(
new StackTraceElement("ClassA", "methodB", "FileC.java", 123),
new StackTraceElement("ClassX", "methodY", "FileZ.java", 456)
);
t.setStackTrace(elements.toArray(new StackTraceElement[0]));
t.printStackTrace();
// Result
// ↓
//SampleException
// at ClassA.methodB(FileC.java:123)
// at ClassX.methodY(FileZ.java:456)
String toString ()
返回此可抛出对象的简短描述。 (机器翻译)
final Throwable t = new SampleException("abcde");
final var str = t.toString();
System.out.println(str); // SampleException: abcde