taquaki-satwo
12/13/2017 - 1:37 PM

try catch finally

JS-try catch finally

try {
  // 例外が発生される可能性がある処理
  var p = permutation(a);
  // 上のコードで例外が発生すると下のコードは実行されない
  p.forEach(function(v) {console.log(v);});
} catch(exception) {
  // 例外発生時の処理
  // exceptionは例外として投げられた値
  if(exception instanceof TypeError) {
    // -> TypeErrorが発生した場合の処理
  } else if (exception instanceof ReferenceError) {
    console.log(exception); // -> ReferenceError: permutation is not defined
  } else {
    // 上記以外の例外が発生した場合の処理
  }
} finally {
  // try,catchが実行された後の処理
}