LoginSignup
12
12

More than 5 years have passed since last update.

Rubyの例外処理に関して色々、復習してみた。

Posted at

Ruby歴2年になった自分が初心に戻って、
例外処理を再復習するために学んだまとめ。

例外処理

rescue 引数なしは Standard Error を補足

begin
    error_method # 例外発生を含むメソッド
rescue => e # standard Error とそのサブクラスを補足。

end

rescue後のraiseで同様のエラーを発生させられる。

begin
    error_method # 例外発生を含むメソッド
rescue => e
    raise # eと同じ内容の例外が発生
end

後置rescueはStandard Errorのみ補足

(例1)
raise rescue puts('error') # error (例外を補足する)

(例2)
raise Exception rescue puts('error') # これはエラーを補足しない。

メソッドで例外を補足する。

def
    error_method
rescue
    ## メソッド呼び出し時に例外発生した場合に補足
end

クラスやモジュールで例外を補足する。

class
    ## 何らかの処理
rescue
    ## クラス定義時に例外発生した場合に補足
end

以上

12
12
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
12
12