Created
November 5, 2011 13:55
-
-
Save pasberth/1341526 to your computer and use it in GitHub Desktop.
例外を細くしたりする感じの
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
| module Kernel | |
| def _throw e=RuntimeError, *args, &blk | |
| if e.kind_of? Exception | |
| $! = e | |
| $@ = caller | |
| throw :exception | |
| else | |
| $! = e.new *args, &blk | |
| $@ = caller | |
| throw :exception | |
| end | |
| end | |
| def _try &blk | |
| catch(:exception) do | |
| ret = blk.call | |
| return ret | |
| end | |
| end | |
| def _catch type=StandardError, &blk | |
| case $! | |
| when type | |
| ret = blk.call $! | |
| else | |
| ret = nil | |
| end | |
| return ret | |
| end | |
| def _else &blk | |
| blk.call unless $! | |
| end | |
| end | |
| _try { | |
| puts "try" | |
| _throw | |
| puts "break" | |
| } | |
| _catch { |e| | |
| puts "EXCEPTION!" | |
| puts e.message | |
| puts $@ | |
| } | |
| _try { | |
| puts "try" | |
| _throw LoadError | |
| puts "break" | |
| } | |
| _catch { |e| | |
| puts "EXCEPTION!" | |
| puts e.message | |
| puts $@ | |
| } | |
| _catch(LoadError) { |e| | |
| puts "LOAD ERROR!" | |
| puts e.message | |
| puts $@ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment