看了一下,个人感觉题目可能会有点主观因素在里面,可能会对某些人产生误导。
以下为个人观点,仅供参考:
首先我们在使用java类的时候就需要弄明白该类被设计出来的目的是什么,需要如何使用,会有什么表现,异常是抛出、内部处理还是忽略等等,只有在了解这些之后才能正确的使用这个类进行编码开发,否则就是满意的写出充满BUG的代码而不自知。
对于ExecutorService中submit()与execute()方法对于异常的处理有人已经总结出来了:
There is a difference concerning exception/error handling.
A task queued with execute() that generates some Throwable will cause the UncaughtExceptionHandler for the Thread running the task to be invoked. The default UncaughtExceptionHandler, which typically prints the Throwable stack trace to System.err, will be invoked if no custom handler has been installed.
On the other hand, a Throwable generated by a task queued with submit() will bind the Throwable to the Future that was produced from the call to submit(). Calling get() on that Future will throw an ExecutionException with the original Throwable as its cause (accessible by calling getCause() on the ExecutionException).
也就是说execute()就是被设计为“吞掉”异常的,我们在使用之前就应该清楚这一点,而不是在模糊不清认为存在异常会抛出的情况下一直使用该类,却在某个时间点突然发现与原来不符而感性的认为execute()方法居然是不可思议的“吞掉”了本该抛出的异常。
这在语言运用的本质上就是本末倒置了。