Ruby中的异常类方法

在这里,我们将看到Exception类的一些方法。我们已经讨论了Exception类的层次结构。异常不过是在运行时出现的异常情况和不需要的情况。在beginend块之间使用Exception类的方法来创建引发和挽救关键字的接口。
创建Exception实例或对象时,它们包含Exceptions的信息,例如其类型,某种可选的描述性字符串以及其他类型的可选信息。

下面列出了Exception类的一些主要方法,以及它们的示范程序代码和语法。

1)异常类方法

它们是例外新的

我新

顾名思义,此方法用于创建Exception类的新实例或对象,并可以选择设置消息字符串。

语法:

Exception.new(message)

示例

=begin
    Ruby program to demonstrate use of new method 
=end

class NewException < StandardError 
attr_reader :myobj

def initialize(myobj) 
  @myobj = myobj 
end
end

begin

raise NewException.new("This is my object created with new"), "This is self made class"
rescue NewException => e 
puts e.message 
puts e.myobj 
end

输出:

This is self made class
This is my object created with new

说明:

在上面的代码中,您可以看到我们借助扩展Standard错误类创建了一个名为NewException的自定义或自制异常,然后借助initialize方法创建了它的构造函数。进入开始结束,我们实际上已经实现了一个用于创建NewException类对象的方法。您可以看到圆括号之间的字符串已发送到构造函数,其余的字符串已发送到message方法。

ii)例外

方法类似,此方法还用于创建Exception类的新实例或对象,并可以选择设置消息字符串。

语法:

Exception.exception(message)

示例

=begin
    Ruby program to demonstrate use of exception method 
=end

class NewException < StandardError 
attr_reader :myobj

def initialize(myobj) 
  @myobj = myobj 
end
end

begin

raise NewException.exception("This is my object created with exception"), "This is self made class"
rescue NewException => e 
puts e.message 
puts e.myobj 
end

输出:

This is self made class
This is my object created with exception

说明:

在上面的代码中,您可以看到,借助于扩展Standard错误类,我们创建了一个名为NewException的自定义或自制异常,然后借助initialize方法创建了它的构造函数。进入开始 结束块,我们实际上已经实现了用于创建NewException类对象的异常方法。您可以看到圆括号之间的字符串被发送到构造函数,其余的字符串被发送到message方法。

2)公共实例方法

我)回溯

此方法用于显示任何backtrace,如果Exception类的对象中存在任何backtrace。这是一种String数组,其中包含名为filename:line:infilename:line的方法backtrace告诉我们错误或异常发生在哪一行。

语法:

exception_object.backtrace

示例

=begin
    Ruby program to demonstrate use of backtrace method
=end 
 
def meth1 
    raise "exception raised! Rescue"
end

def meth2  
    meth1() 
end

begin 
    meth2()  
    rescue => meth_Details 
    puts meth_Details.backtrace.join("\n") 
end

输出:

main.rb:6:in `meth1'
main.rb:10:in `meth2'
main.rb:14:in `<main>'

说明:

在上面的代码,你可以看到,我们的程序显示有关信息回溯其发生的帮助下对象回溯方法。

ii)消息

此方法是一个公共实例方法,用于打印声明对象时可能存储的消息。

语法:

Exception_object.message

示例

=begin
    Ruby program to demonstrate use of new method 
=end

class NewException < StandardError 
    attr_reader :myobj
    
    def initialize(myobj) 
      @myobj = myobj 
    end
end

begin
    raise NewException.new("This is my object created with new"), "This is self made class"
    rescue NewException => e 
    puts e.message 
    puts e.myobj 
end

输出:

This is self made class
This is my object created with new

说明:

在上面的代码中,您可以观察到借助message方法,我们可以看到与创建对象时传递的Exception类的对象相关的消息。

iii)set_backtrace

此方法是Exception类的公共实例方法,用于在Exception类的对象中设置与回溯相关的信息。

示例

=begin
    Ruby program to demonstrate use of set_backtrace method
=end 
class ErrorCreate
  def self.new(error, message = nil, backtrace: caller)
    exception = error.new(message)
    exception.set_backtrace(backtrace)
    exception
  end
end
ErrorCreate.new(StandardError,"Hello from includehelp.com", backtrace: caller)

输出:

#<StandardError: Hello from includehelp.com>

说明:

在上面的代码中,您可以看到我们创建了自己的错误并在其中实现了set_backtrace。借助这种方法,我们可以设置有关错误的信息。

iv)to_s

这个消息是用于返回沿着与所述的物体通过的消息的公共实例方法的异常,当类异常被创建的对象。如果未传递任何消息,则返回对象的名称。

语法:

Exception_object.to_s

示例

=begin
    Ruby program to demonstrate use of to_s method 
=end

begin 
    raise "Exception Raised. From includehelp.com"
    rescue Exception => e 
    puts e.to_s 
end

输出:

Exception Raised. From includehelp.com

v)检查

这是一个公共实例方法,用于返回异常类的名称以及消息。

示例

=begin
    Ruby program to demonstrate use of inspect method 
=end

class NewException < StandardError 
    attr_reader :myobj
    
    def initialize(myobj) 
      @myobj = myobj 
    end
end

begin
    raise NewException.new("This is my object created with new"), "This is self made class"
    rescue NewException => e 
    puts e.message 
    puts e.myobj
    puts e.inspect 
end

输出:

This is self made class
This is my object created with new
#<NewException: This is self made class>

说明:

在上面的代码中,您可以观察到我们已经创建了一个NewException类的对象,并且inspect方法正在通知我们有关类的名称以及与该对象关联的消息的信息。

vi)==运算符

这是一个公共实例方法,用于比较Exceptions类。如果两个异常属于同一类,则此方法返回true,否则返回false。

语法:

Object_of_Exception == Object2_of_Exception

示例

=begin
    Ruby program to demonstrate use of == method 
=end

class NewException < StandardError 
    attr_reader :myobj
    
    def initialize(myobj) 
      @myobj = myobj 
    end
end

begin
    raise NewException.new("This is my object created with new"), "This is self made class"
    rescue NewException => e 
    obj = NewException.new("Hello")
    obj1 = NewException.new("Bye")
    puts e.message 
    puts e.myobj
    puts obj1 == obj
end

输出:

This is self made class
This is my object created with new
true

说明:

在上面的输出中,您可以观察到我们已经创建了两个NewException类的对象,并将它们与==进行比较后,结果为true。