Java : Exception 示例

Java 中的 Exception (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 Exception 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

Exception 类及其子类是 Throwable 的一种形式,用于指示合理的应用程序可能想要捕获的条件。 (机器翻译)

Class diagram

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 ex) {

            final Exception e = ex;
            e.printStackTrace();

            // Result
            // ↓
            //SampleException
            //	at Main.func2 ...
            //	at Main.func1 ...
            //	at Main.main ...
            // ...
        }
    }

    private static void func1() throws SampleException {
        func2();
    }

    private static void func2() throws SampleException {
        throw new SampleException();
    }
}

Constructors

Exception ()

构造一个新的异常,其详细消息为 null。 (机器翻译)

final Exception e = new SampleException();
System.out.println(e); // SampleException

Exception (String message)

构造具有指定详细消息的新异常。 (机器翻译)

final Exception e = new SampleException("abcde");
System.out.println(e); // SampleException: abcde
System.out.println(e.getMessage()); // abcde

Exception (String message, Throwable cause)

构造具有指定详细消息和原因的新异常。 (机器翻译)

final Exception cause = new SampleException("XYZ");
final Exception e = new SampleException("abcde", cause);

System.out.println(e); // SampleException: abcde
System.out.println(e.getMessage()); // abcde

System.out.println(e.getCause()); // SampleException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ

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

构造一个新的异常,并指定详细消息、原因、启用或禁用抑制以及启用或禁用可写堆栈跟踪。 (机器翻译)

final Exception cause = new SampleException("XYZ");
final Exception e = new SampleException("abcde", cause, true, true);

System.out.println(e); // SampleException: abcde
System.out.println(e.getCause()); // SampleException: XYZ

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

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

System.out.println(e.getStackTrace().length > 0); // true
// enableSuppression = false
final Exception cause = new SampleException("XYZ");
final Exception e = new SampleException("abcde", cause, false, true);

System.out.println(e); // SampleException: abcde
System.out.println(e.getCause()); // SampleException: XYZ

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

System.out.println(e.getStackTrace().length > 0); // true
// writableStackTrace = false
final Exception cause = new SampleException("XYZ");
final Exception e = new SampleException("abcde", cause, true, false);

System.out.println(e); // SampleException: abcde
System.out.println(e.getCause()); // SampleException: XYZ

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

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

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

Exception (Throwable cause)

构造一个具有指定原因和详细消息(cause==null ? null : cause.toString())的新异常(通常包含原因的类和详细消息)。 (机器翻译)

final Exception cause = new SampleException("XYZ");
final Exception e = new SampleException(cause);

System.out.println(e); // SampleException: SampleException: XYZ
System.out.println(e.getMessage()); // SampleException: XYZ

System.out.println(e.getCause()); // SampleException: 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

请参阅下面的链接。


相关文章

To top of page