- 追加された行はこの色です。
- 削除された行はこの色です。
&tag(Ruby/例外処理);
*目次 [#j4898261]
#contents
*参考情報 [#c7abcc9a]
*基礎知識 [#s6410827]
-$!が例外オブジェクト、$@がスタックトレースを表す。
-以下の例外の型を指定しない方法で例外を補足する場合、StandardErrorのサブクラスしか補足できない。
#pre{{
begin
rescue => e
end
}}
-Exceptionを指定すれば全ての例外が補足できる。
#pre{{
begin
resucue Exception => e
end
}}
-Exceptionをrescueすると、Ctrl-Cなどもtrapしてしまいこまったことになるかも。その場合[[exception - Capturing Ctrl-c in ruby - Stack Overflow:http://stackoverflow.com/questions/2089421/capturing-ctrl-c-in-ruby]]が参考になる。
#pre{{
rescue Exception => e
# ...
raise
end
}}
#pre{{
rescue SystemExit, Interrupt
raise
rescue Exception => e
#...
end
}}
*例外の表示方法 [#g6351fa1]
-[[The simple way to print exceptions in Ruby | coderrr:http://coderrr.wordpress.com/2008/11/07/the-simple-way-to-print-exceptions-in-ruby/]]
#pre{{
rescue Exception => e
puts e.message, e.backtrace
end
rescue Exception
puts $!, $@
end
}}