2014-04-18 95 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有關如何處理和引發異常的更多信息。