本帖最后由 余烬之中 于 2014-2-23 13:01 编辑
话说太短的话可以加功能嘛……
增强实用性 比如if $RGSS_SCRIPTS[0][3].include?("8e860c510b6653364928f6adf99b3283") $RGSS_SCRIPTS.each do |s| s[3].gsub!("rescue!"){"rescue exstore "} end module Reraise $exstack = [] # 栈 # 重抛出异常 def reraise(ex) raise ex.class, ex.message, ex.backtrace end # 压栈 def exstore $exstack.push($!) end # 退栈 def exlast return true if $exstack.empty? reraise $exstack.pop end end include Reraise else warning = defined?(Audio.setup_midi) ? method(:msgbox) : method(:print) warning.call "请将此脚本置于最顶端 → →" end
if $RGSS_SCRIPTS[0][3].include?("8e860c510b6653364928f6adf99b3283")
$RGSS_SCRIPTS.each do |s|
s[3].gsub!("rescue!"){"rescue exstore "}
end
module Reraise
$exstack = [] # 栈
# 重抛出异常
def reraise(ex)
raise ex.class, ex.message, ex.backtrace
end
# 压栈
def exstore
$exstack.push($!)
end
# 退栈
def exlast
return true if $exstack.empty?
reraise $exstack.pop
end
end
include Reraise
else
warning = defined?(Audio.setup_midi) ? method(:msgbox) : method(:print)
warning.call "请将此脚本置于最顶端 → →"
end
然后测试用例class TEST def a puts "a pre" raise NoMethodError, "A" rescue! puts "a done" end def b puts "b pre" raise NoMethodError, "B" rescue! puts "b done" end end t = TEST.new #~ # 用例一 #~ puts "picnic~" #~ t.a #~ puts "have a break~" #~ exlast rescue puts "a small problem...#{$!}" #~ puts "let's go home!" #~ puts "======================================" #~ puts "picnic~ (2nd)" #~ t.a #~ puts "have a break~" #~ exlast #rescue puts "a small problem...#{$!}" #~ puts "let's go home! (you've got no chance!)" # 用例二 puts "picnic~" t.a t.b loop do (r = exlast) rescue puts "a small problem...#{$!}" break if r end
class TEST
def a
puts "a pre"
raise NoMethodError, "A" rescue!
puts "a done"
end
def b
puts "b pre"
raise NoMethodError, "B" rescue!
puts "b done"
end
end
t = TEST.new
#~ # 用例一
#~ puts "picnic~"
#~ t.a
#~ puts "have a break~"
#~ exlast rescue puts "a small problem...#{$!}"
#~ puts "let's go home!"
#~ puts "======================================"
#~ puts "picnic~ (2nd)"
#~ t.a
#~ puts "have a break~"
#~ exlast #rescue puts "a small problem...#{$!}"
#~ puts "let's go home! (you've got no chance!)"
# 用例二
puts "picnic~"
t.a
t.b
loop do
(r = exlast) rescue puts "a small problem...#{$!}"
break if r
end
|