title: Unhandled Exception in Dart
body: <p>I've written a webserver in Dart and have a question regarding exceptions. In my HttpServer requesthandler I've added a try-catch block around the entire method:</p>
  
  <pre><code>try{
   ...
  } catch(e) {
   ...
  }
  </code></pre>
  
  <p>So I expected that this would prevent any client request from crashing the webserver. The problem is that it <strong>can</strong> crash when certain exceptions are thrown from within this block (heavily nested in other modules, but still initiated from this block). The following is an example of such a exception:</p>
  
  <pre><code>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.&lt;anonymous closure&gt; (dart:io:1307:29)
    #5      _FutureImpl.transform.&lt;anonymous closure&gt; (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.&lt;anonymous closure&gt; (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.&lt;anonymous closure&gt; (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.&lt;anonymous closure&gt; (dart:io:6934:23)
   #14     _ReceivePortImpl._handleMessage (dart:isolate-patch:37:92)
  </code></pre>
  
  <p>Why doesn't this get catched in the try-catch block? It is thrown in code that is called from within it (even though it isn't shown in the stacktrace).</p>
  
  <p>I expect that I've missed something about how exceptions work in Dart, so I hope you can enlighten me :)</p>