Java : Exception with Examples

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


Summary

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

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 ()

Constructs a new exception with null as its detail message.

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

Exception (String message)

Constructs a new exception with the specified detail message.

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

Exception (String message, Throwable cause)

Constructs a new exception with the specified detail message and 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)

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

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)

Constructs a new exception 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 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

Please see the link below.


Related posts

To top of page