begin
# something which might raise an exception
rescue SomeExceptionClass => some_variable
# code that deals with some exception
ensure
# ensure that this code always runs
end
这里,def作为begin语句:
def
# something which might raise an exception
rescue SomeExceptionClass => some_variable
# code that deals with some exception
ensure
# ensure that this code always runs
end
5条答案
按热度按时间dm7nw8vv1#
方法“def”可以用作“开始”语句:
nom7f22z2#
您也可以在线救援:
将打印出“例外!“since 'String can't be coerced into Fixnum'
bwntbbo33#
我在ActiveRecord验证中经常使用def / rescue组合:
我认为这是非常精简的代码!
ktecyv1j4#
示例:
这里,
def
作为begin
语句:oiopk7p55#
奖励!你也可以用其他类型的块来做这个。例如:
在
irb
中输出: