Thread的UncaughtExceptionHandler
kotlin
1 | fun mainFunction() { |
最终输出是:
plaintext
1 | end |
try catch不能捕获在trycatch括号内开的子线程抛出的异常,可以为线程设置一个 UncaughtExceptionHandler
。
我联想到Coroutine中,try catch同样无法捕获括号内新开的子协程内抛出的错误,与之不同的是,协程的错误不是定义子协程的exception handler来处理,而是可以为TopLevelScope或Top level coroutine定义异常处理器。
参考资料:
评论