CoffeeScriptのtry/catchの短縮表記 質問する

CoffeeScriptのtry/catchの短縮表記 質問する

次のようなコードを時々書きます:

try doSomething()
catch e
  handleError e

これは、きれいでクリーンな CoffeeScript コードとは本来あるべき姿ではありません。

次のように書く方法はありますか:

try doSomething()
catch e handleError e   #<-- will not compile

これにより、try/catch ステートメントのコード行が約 33% 削減されます ;)

ベストアンサー1

try/catch ワンライナーの記述は、次のキーワードを使用して、if-then ワンライナーや loop ワンライナーと同じように機能しますthen

try doSomething()
catch e then handleError e
finally cleanUp()

必要に応じて、1 行にまとめることもできます。

try doSomething() catch e then handleError e finally cleanUp()

おすすめ記事