Purus Education
EN
Lesson 13

エラー処理

Lesson 13 — エラー処理

English

構文

エラーは throw で投げ、try / catch(必要なら finally)で処理します:

fn risky
  throw new Error[//;something went wrong;//]

try
  risky[]
catch e
  console.log[e.message]
finally
  cleanup[]
try は式としても使えます:
const result be try
  risky[]
catch e
  default-value

課題

1. safe-divideb eq 0 のとき new Error[//;division by zero;//] を throw するようにする。

2. try-divide が呼び出しを try/catch で包み、エラー時に //;error: [e.message];// を返すようにする。

確認

npm test 13

参考

Loading editor...
Ctrl+Enterで実行