2014-04-18 47 views
0

我希望能够检查某行代码是否生成ArgumentError如何检查是否引发了ArgumentError?

喜欢的东西:

if this.ArgumentError? 
    puts "there was an error" 
else 
    puts "no error" 
end 

我也搜索了相关文件,并没有发现任何东西,似乎工作。

这可能吗?

回答

2

您必须使用异常处理机制。例如:

begin 
    # here is the code that could raise ArgumentError 
rescue ArgumentError 
    puts "there was an error" 
else 
    puts "no error" 
end 

参考official documentation有关如何处理和引发异常的更多信息。