0
0

More than 1 year has passed since last update.

Future interfaceでexceptionをキャッチする

Last updated at Posted at 2023-01-17

f.get()したときに、ExecutionExceptionが発生する
ExecutionExceptionがsubmit()で発生したExceptionを保有している

class SampleException extends Exception {
    public SampleException(String message) {
        super(message);
    }

    public SampleException() {
    }
}
public class Outer {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService e = Executors.newSingleThreadExecutor();
        Future f = e.submit(() -> {throw new SampleException("exxxxx");});
        e.shutdown();
        try {
            System.out.println(f.get());
        } catch (ExecutionException ex) {
            System.out.println(ex.getMessage());
        }
    }
}
com.mycompany.mavenproject1.SampleException: exxxxx
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0