Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用回溯构造异常_Ruby - Fatal编程技术网

Ruby 使用回溯构造异常

Ruby 使用回溯构造异常,ruby,Ruby,ruby中真的没有采取回溯的异常构造函数吗?我想用回溯信息创建一个异常,但我看不到这样做的方法。现在我正在这样做: exception = create_error ArgumentError, "There is an error here", caller 使用如下定义的create_error方法,即构造异常,然后分两步设置回溯: def create_error(error_class, msg, bt) e = error_class.new(msg) e.set

ruby中真的没有采取回溯的异常构造函数吗?我想用回溯信息创建一个异常,但我看不到这样做的方法。现在我正在这样做:

exception = create_error ArgumentError, "There is an error here", caller
使用如下定义的create_error方法,即构造异常,然后分两步设置回溯:

  def create_error(error_class, msg, bt)
    e = error_class.new(msg)
    e.set_backtrace(bt)
    e
  end

注意,我不想在这种情况下“提高”。我遗漏了什么吗?

它确实有用。在您的示例中,
exception.backtrace
将打印出您提供的backtrace信息。是的,我知道它可以按编写的方式工作。我的问题是,是否有一个构造函数接受回溯,或者以某种方式在一行中创建一个带有回溯的异常