Exception is programmatically recoverable. Its subclass RuntimeException indicates a programming error and is usually not to be caught as well. Throwable is super class of Exception as well as Error. In normal cases we should always catch sub-classes of Exception, so that the root cause doesn't get lost.
如果下面的写法捕获不到异常,可以使用case e:Throwable尝试捕获所有异常
catch { case e:Exception => { e.printStackTrace() } }
Thowable
catches really everything even ThreadDeath which gets thrown by default to stop a thread from the now deprecated Thread.stop()
method. So by catching Throwable
you can be sure that you'll never leave the try block without at least going through your catch block, but you should be prepared to also handle OutOfMemoryError
and InternalError
or StackOverflowError
.
Catching Throwable
is most useful for outer server loops that delegate all sorts of requests to outside code but may itself never terminate to keep the service alive.