Created
October 12, 2015 06:17
-
-
Save mitsuru793/e2da2a434b3220f213d6 to your computer and use it in GitHub Desktop.
rubyの例外処理を試してみる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| begin | |
| raise 'エラーが発生しました' | |
| rescue => e | |
| puts e.message | |
| end | |
| # => エラーが発生しました | |
| # Exceptionだとrescueで型指定しないとcatchできない | |
| begin | |
| raise Exception.new('エラーが発生しちゃった') | |
| rescue => e | |
| puts e.message | |
| end | |
| # => `<main>': エラーが発生しちゃった (Exception) | |
| # rescueで型指定をしているのでExceptionをcatchできる | |
| begin | |
| raise Exception.new('エラーが発生したんだ') | |
| rescue Exception => e | |
| puts e.message | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment