Программисты JAVA общаются здесь
-
Anonymous
Почему мы используем, попробуйте поймать, даже когда мы бросаем исключение [закрыто]
Сообщение
Anonymous »
Код: Выделить всё
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(1);
Callable callable1 = () -> {
Thread.sleep(1000);
System.out.println("task 1");
return 1;
};
Callable callable2 = () -> {
Thread.sleep(1000);
System.out.println("task 2");
return 2;
};
List list = Arrays.asList(callable1 , callable2);
List futures = executor.invokeAll(list , 1, TimeUnit.SECONDS) ;
for(Future f : futures){
try {
System.out.println(f.get());
} catch (Exception e) {
}
}
executor.shutdown();
System.out.println("hello world");
}
}
Я попытался бросить Interruptexception, CallableException, executionExcect>
Подробнее здесь:
https://stackoverflow.com/questions/797 ... -exception
1757987651
Anonymous
[code]public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(1);
Callable callable1 = () -> {
Thread.sleep(1000);
System.out.println("task 1");
return 1;
};
Callable callable2 = () -> {
Thread.sleep(1000);
System.out.println("task 2");
return 2;
};
List list = Arrays.asList(callable1 , callable2);
List futures = executor.invokeAll(list , 1, TimeUnit.SECONDS) ;
for(Future f : futures){
try {
System.out.println(f.get());
} catch (Exception e) {
}
}
executor.shutdown();
System.out.println("hello world");
}
[/code]
}
Я попытался бросить Interruptexception, CallableException, executionExcect>
Подробнее здесь: [url]https://stackoverflow.com/questions/79765517/why-do-we-use-try-catch-even-when-we-throw-exception[/url]