我在Dart写了一个网络服务器,并对异常有疑问.在我的HttpServer requesthandler中,我在整个方法中添加了一个try-catch块:
try{
...
} catch(e) {
...
}
Run Code Online (Sandbox Code Playgroud)
所以我希望这可以防止任何客户端请求崩溃Web服务器.问题是当从这个块中抛出某些异常时它会崩溃(严重嵌套在其他模块中,但仍然从这个块启动).以下是此类例外的示例:
Unhandled exception:
FutureUnhandledException: exception while executing Future
Illegal argument(s)
original stack trace:
#0 _StringBase._createFromCodePoints (dart:core-patch:1403:3)
#1 _StringBase.createFromCharCodes (dart:core-patch:1400:33)
#2 String.String.fromCharCodes (dart:core-patch:1788:43)
#3 _StringDecoderBase.decoded (dart:io:6485:12)
#4 _File.readAsString.<anonymous closure> (dart:io:1307:29)
#5 _FutureImpl.transform.<anonymous closure> (bootstrap:881:37)
#0 _FutureImpl._complete (bootstrap:844:11)
#1 _FutureImpl._complete (bootstrap:848:5)
#2 _FutureImpl._setException (bootstrap:873:14)
#3 _CompleterImpl.completeException (bootstrap:948:30)
#4 _FutureImpl.transform.<anonymous closure> (bootstrap:884:36)
#5 _FutureImpl._complete (bootstrap:840:19)
#6 _FutureImpl._complete (bootstrap:848:5)
#7 _FutureImpl._setValue (bootstrap:862:14)
#8 _CompleterImpl.complete (bootstrap:945:26)
#9 _File.readAsBytes.<anonymous closure> (dart:io:1281:25)
#10 _BaseDataInputStream._checkScheduleCallbacks.issueCloseCallback (dart:io:6345:59)
#11 _Timer._createTimerHandler._handleTimeout (dart:io:6918:28)
#12 _Timer._createTimerHandler._handleTimeout (dart:io:6926:7)
#13 _Timer._createTimerHandler.<anonymous closure> (dart:io:6934:23)
#14 _ReceivePortImpl._handleMessage (dart:isolate-patch:37:92)
Run Code Online (Sandbox Code Playgroud)
为什么这不会在try-catch块中被捕获?它被抛入从其中调用的代码中(即使它没有显示在堆栈跟踪中).
我希望我错过了关于Dart中异常如何工作的一些内容,所以我希望你能开导我:)