相关疑难解决方法(0)

Ruby自定义错误类:message属性的继承

我似乎无法找到有关自定义异常类的更多信息.

我所知道的

您可以声明自定义错误类并让它继承StandardError,因此它可以是rescued:

class MyCustomError < StandardError
end
Run Code Online (Sandbox Code Playgroud)

这允许您使用以下方法来提高它:

raise MyCustomError, "A message"
Run Code Online (Sandbox Code Playgroud)

然后,在救援时获取该消息

rescue MyCustomError => e
  puts e.message # => "A message"
Run Code Online (Sandbox Code Playgroud)

我不知道的

我想给我的异常一些自定义字段,但我想message从父类继承该属性.我发现阅读关于这个主题@message不是异常类的实例变量,所以我很担心我的遗产将无法正常工作.

谁能给我更多细节呢?如何使用object属性实现自定义错误类?以下是正确的:

class MyCustomError < StandardError
  attr_reader :object
  def initialize(message, object)
    super(message)
    @object = object
  end
end
Run Code Online (Sandbox Code Playgroud)

然后:

raise MyCustomError.new(anObject), "A message"
Run Code Online (Sandbox Code Playgroud)

要得到:

rescue MyCustomError => e
  puts e.message # => "A message"
  puts e.object # => anObject
Run Code Online (Sandbox Code Playgroud)

它会起作用,如果确实如此,这是正确的做事方式吗?

ruby inheritance exception-handling exception custom-exceptions

94
推荐指数
4
解决办法
6万
查看次数

如何在Rails中引发异常,使其行为与其他Rails异常一样?

我想提出一个异常,以便它正常的Rails异常做同样的事情.特别是,在开发模式下显示异常和堆栈跟踪,并在生产模式下显示"我们很抱歉,但出现了问题"页面.

我尝试了以下方法:

raise "safety_care group missing!" if group.nil?
Run Code Online (Sandbox Code Playgroud)

但它只是写入"ERROR signing up, group missing!"development.log文件

exception-handling ruby-on-rails exception

89
推荐指数
3
解决办法
13万
查看次数

捕获rails控制器中的所有异常

有没有办法在rails控制器中捕获所有未捕获的异常,如下所示:

def delete
  schedule_id = params[:scheduleId]
  begin
    Schedules.delete(schedule_id)
  rescue ActiveRecord::RecordNotFound
    render :json => "record not found"
  rescue ActiveRecord::CatchAll
    #Only comes in here if nothing else catches the error
  end
  render :json => "ok"
end
Run Code Online (Sandbox Code Playgroud)

谢谢

ruby-on-rails

87
推荐指数
4
解决办法
9万
查看次数

ActiveRecord :: StatementInvalid:PG InFailedSqlTransaction

我正在尝试创建一个ActiveRecord Object.But我在创建它时遇到此错误.

(0.1ms)  ROLLBACK
ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR:  current transaction is       aborted, commands ignored until end of transaction block
Run Code Online (Sandbox Code Playgroud)

关于这个问题的任何想法的人.

postgresql activerecord ruby-on-rails rollback pg

66
推荐指数
5
解决办法
3万
查看次数

如何发出"尚未实施"的信号?

在初始绘制新gem时,我需要将一些方法实现留空(将在下一个实现)

因此,我想发出一个"尚未实施"的例外情况

我想知道是否有一个特定于Ruby语言的最佳实践标准约定来编写这种占位符/异常.

即:像:

ruby coding-style exception-handling conventions

59
推荐指数
2
解决办法
2万
查看次数

如何在Ruby中向异常消息添加信息?

如何在不更改ruby中的类的情况下向异常消息添加信息?

我目前使用的方法是

strings.each_with_index do |string, i|
  begin
    do_risky_operation(string)
  rescue
    raise $!.class, "Problem with string number #{i}: #{$!}"
  end
end
Run Code Online (Sandbox Code Playgroud)

理想情况下,我还想保留回溯.

有没有更好的办法?

ruby exception-handling exception

55
推荐指数
4
解决办法
2万
查看次数

Ruby相当于python尝试

我是编码的主要菜鸟.我开始学习Python,但不是粉丝,而是转向我喜欢的Ruby.现在我正在尝试将一些python代码转换为ruby而我陷入了python的"尝试"是一个相当于python的"尝试"的红宝石

ruby python language-comparisons try-catch

42
推荐指数
2
解决办法
2万
查看次数

如何在Ruby中拯救eval?

我正在试图找出如何解决eval()在Ruby 1.8.6中使用代码时出现的语法错误.

我希望以下Ruby代码:

#!/usr/bin/ruby

good_str = "(1+1)"
bad_str = "(1+1"    # syntax error: missing closing paren

begin
    puts eval(good_str)
    puts eval(bad_str)
rescue => exc
    puts "RESCUED!"
end
Run Code Online (Sandbox Code Playgroud)

运行时产生以下结果:

2
RESCUED!
Run Code Online (Sandbox Code Playgroud)

相反,我得到的是:

2
eval_rescue.rb:8: (eval):1: compile error (SyntaxError)
(eval):1: syntax error, unexpected $end, expecting ')'
Run Code Online (Sandbox Code Playgroud)

似乎eval方法引发的SyntaxError正在eval中的某个地方被拯救,而我没有机会自己处理它.

任何人都知道如何获得我想要的行为(即,我的'rescue'条款从'eval'中捕获错误)?

ruby eval exception rescue

39
推荐指数
2
解决办法
2万
查看次数

如何在ruby中显示错误类型?

在以下代码中

begin
 raise StandardError, 'message'
 #some code that raises a lot of exception
rescue StandardError
 #handle error
rescue OtherError
 #handle error
rescue YetAnotherError
 #handle error
end
Run Code Online (Sandbox Code Playgroud)

我想打印一个警告,说明错误的类型和消息,而不是为每个救援条款添加print语句,例如

begin
 raise StandardError, 'message'
 #some code that raises a lot of exception
rescue StandardError
 #handle error
rescue OtherError
 #handle error
rescue YetAnotherError
 #handle error
???
 print "An error of type #{???} happened, message is #{???}"
end
Run Code Online (Sandbox Code Playgroud)

ruby exception

27
推荐指数
3
解决办法
3万
查看次数

这是默默忽略Ruby异常的最短路径

我正在寻找这样的东西:

raise Exception rescue nil
Run Code Online (Sandbox Code Playgroud)

但我发现的最短路径是:

begin
  raise Exception
rescue Exception
end
Run Code Online (Sandbox Code Playgroud)

ruby exception-handling

24
推荐指数
3
解决办法
2万
查看次数