Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created November 5, 2011 13:55
Show Gist options
  • Select an option

  • Save pasberth/1341526 to your computer and use it in GitHub Desktop.

Select an option

Save pasberth/1341526 to your computer and use it in GitHub Desktop.
例外を細くしたりする感じの
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