ruby 怎么拋異常
Ruby異常處理 (Ruby Exception Handling)
Exceptions are abnormal conditions arising in the code segment at runtime in the form of objects. There are certain predefined Exception classes whereas we can create our exception known as Custom exception. Accepts distorts the normal flow of the program at runtime. Exception handling is the process of handling such abnormal conditions or you can also refer them as unwanted events. There is a distinct class known as Exception class in Ruby which contains method specially defined to deal with the unexpected events.
異常是在運行時在代碼段中以對象形式出現的異常情況。 有某些預定義的Exception類,而我們可以創建稱為Custom異常的異常。 接受會在運行時扭曲程序的正常流程。 異常處理是處理此類異常情況的過程,或者您也可以將其稱為有害事件。 Ruby中有一個獨特的類稱為Exception類,其中包含專門定義用于處理意外事件的方法。
To handle an exception, you can take help from raise and rescue block. The general syntax of Exception handling is given below:
要處理異常 ,您可以從引發和營救塊獲得幫助。 異常處理的一般語法如下:
begin
raise
# block where exception raise
rescue
# block where exception rescue
end
First let us see a code which is creating an abnormal condition:
首先讓我們看一下創建異常情況的代碼:
a = 12
b = a/0
puts "Hello World"
puts b
The above Ruby code will give you the following error:
上面的Ruby代碼將給您以下錯誤:
check.rb:7:in '/': divided by 0 (ZeroDivisionError)
You can observe that when an Exception is raised, it disrupts the flow of instructions and statements will not execute which are written after the same statement having Exception.
您可以觀察到,引發Exception時,它會中斷指令流,并且不會執行在具有Exception的同一條語句之后編寫的語句。
We can modify the above code in the following way to avoid the execution distortion of the rest of the instructions.
我們可以通過以下方式修改上述代碼,以避免其余指令的執行失真。
=begin
Ruby program to show Exception Handling.
=end
begin
a = 12
raise
b = a/0
rescue
puts "Exception rescued"
puts "Hello World"
puts b
end
Output
輸出量
Exception rescued
Hello World
In the above program, you can observe that we are writing the code inside the begin...end block. We have put the statement inside "raise" which may raise any kind of abnormality. We have used rescue statement to handle that exception. You can see that the exception raised is not affecting the rest of the instruction. We are getting "Hello World" printed at the end.
在上面的程序中,您可以觀察到我們正在在begin ... end塊內編寫代碼。 我們將語句放在“ raise”中 ,這可能引起任何異常。 我們已經使用了搶救聲明來處理該異常 。 您可以看到引發的異常不會影響指令的其余部分。 我們將在最后打印“ Hello World” 。
We can create our Exception known as Custom Exception with the help of any user defined method. Let us one of its examples for a better understanding of the implementation:
我們可以創建例外稱為自定義異常與任何用戶定義的方法的幫助。 讓我們為更好地理解實現示例之一:
=begin
Ruby program to show Exception Handling.
=end
def exception_arised
begin
puts 'Hello! Welcome to the Includehelp.com.'
puts 'We are still safe from Exception'
# using raise to generate an exception
raise 'Alas! Exception Generated!'
puts 'After Exception created'
# using Rescue method to handle exception
rescue
puts 'Hurrah! Exception handled! We are safe now'
end
puts 'We are out of begin!'
end
# invoking method
exception_arised
Output
輸出量
Hello! Welcome to the Includehelp.com. We are still safe from Exception
Hurrah! Exception handled! We are safe now
We are out of begin!
In the above code, we are creating our exception with the help of a user-defined method 'exception_arised'. We are then invoking it. The above is an example of Custom exception.
在上面的代碼,我們正在創造我們的“exception_arised”用戶定義的方法的幫助下除外 。 然后,我們正在調用它。 以上是Custom異常的示例 。
翻譯自: https://www.includehelp.com/ruby/exception-handling-in-ruby.aspx
ruby 怎么拋異常