LoginSignup
4
1

More than 5 years have passed since last update.

Futureの中で発生した例外を放置した場合を確認した

Last updated at Posted at 2017-12-15

Futureのなかで例外が発生して、対処しなかった時の確認をした。
Dartが良さげに対処すると聞いたので、確認したのが主な理由。

Dart

$ dart -version
Dart VM version: 1.24.2 (Thu Jun 22 08:42:17 2017) on "macos_x64"
main() {
  (() async => throw 'error')();
}

実行結果

Unhandled exception:
error
#0      main.<anonymous closure> (hoge.dart:2:16)
<asynchronous suspension>
#1      main (file:///Users/eiel/hoge.dart:2:30)
#2      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:265)
#3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:151)

ハンドルしてない例外があるってでるし、その箇所もわかる。

ちゃんとハンドリングした場合の確認

main() {
  (() async => throw 'error')()
    .catchError(print);
}

実行結果

error

Node.jsの場合

$ node -v
v8.9.3

new Promise(() => { throw 'error' })

実行結果

(node:9008) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): goro
(node:9008) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

残念ながら場所まではわからない

ちゃんとハンドリングした場合の確認

new Promise(() => { throw 'error' })
  .catch(console.log)
error

そういえば、以下のようにかいても良い気がする

(async () => { throw 'error' })()

Scala

$ scala -version
Scala code runner version 2.12.4 -- Copyright 2002-2017, LAMP/EPFL and Lightbend, Inc.
import scala.concurrent._
import ExecutionContext.Implicits.global
import scala.concurrent.duration._

Future(throw new Exception("error"))

実行結果

なにも起きない

ハンドリングしてみる。

import ExecutionContext.Implicits.global
import scala.concurrent.duration._

Future(throw new Exception("error"))
  .recover{ case e => e.getMessage }.foreach(println)

実行結果

error
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1