Skip to content

Instantly share code, notes, and snippets.

@mitsuru793
Created October 12, 2015 06:17
Show Gist options
  • Select an option

  • Save mitsuru793/e2da2a434b3220f213d6 to your computer and use it in GitHub Desktop.

Select an option

Save mitsuru793/e2da2a434b3220f213d6 to your computer and use it in GitHub Desktop.
rubyの例外処理を試してみる
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