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 怎么抛异常