如何通过一条救援声明尽快捕获所有异常

我知道如何捕捉异常,但我们所做的是在代码的可疑部分之后放置“救援”。 如果你有很多函数通过mysql2 gem发送一个查询到mysql并且你想要捕获它们的异常。 你可以做的一件事就是在他们每个人中加上一个“救援”声明。 但我只想通过一个救援声明来做到这一点。 因此,我在代码结束时加入了一个“救援”,并将所有代码放在“开始”和“结束”中,但它不起作用。

这里是我的代码,正如你看到的那样,在mysql查询中有一个问题,并且仅仅因为“rescue”是文件结尾,它不会捕获异常,但是当我把它放在那个查询之后它就起作用了。

    require 'mysql2'
require 'colored'

begin

def log(string)
  p "["+string.cyan+"]"
end

def err
  p "["+"FAIL".red+"]"
end

def done
  p "["+"DONE".red+"]"
end

class SqlClient
  def initialize()
    log "SqlClient/initialize"
    puts "Host: n"
    @host = gets.strip
    puts "User: n"
    @user = gets.strip
    puts "Pass: n"
    @pass = gets.strip
    @client = Mysql2::Client.new(host: @host , username: @user , password: @pass)

  end

  def list_databases()
    puts "We are listing your databases(not just projects) n n n "
    @client.query("ELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA").each do |row|
      p row["SCHEMA_NAME"]
    end

    puts "n n n"

  end

end

    rescue Mysql2::Error
    err
    abort

end


    `query': You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near 'ELECT SCHEMA_NAME FROM 
INFORMATION_SCHEMA.SCHEMATA' at line 1 (Mysql2::Error)

我不是在寻找像这样的东西:

begin
  # my code
rescue # this line is right after the code which is going to have problem and we catch it.

end

我正在寻找这样的东西:

begin
  # first method
  # second method
  # thrid method
  # rest of code and etc ...
  # now this is end of file: 
rescue
end

但正如你在我的代码中看到的那样,它不起作用。

更新:我在这里发现了一个类似的问题,似乎没有答案:| 也许这是一种红宝石的弱点。


如果你想看到任何错误,只需使用e例如

begin
  # your call to a method of Mysql2 gem. for example:
  client = Mysql2::Client.new(:host => "localhost", :username => "root", etc...)

rescue => e
  puts e.message
  puts e.backtrace.inspect 

end

为了捕获每个异常,您需要将每个方法调用与begin rescue end 。 当引发一个异常时,它会退出执行,所以它不会触发下一个方法。

为了抓住所有的错误,我想我会做这样的事情。 请记住,这是丑陋的,我建议你要这样做,但是...如果你想尝试,也许尝试这样的事情:

all_errors = []

# first method you call
begin
  # some error might happen here
  first_response = Mysql2::Client.new(:host => "localhost", :username => "root", etc...)
rescue => e
  all_errors << e
end

# second method you call
begin
  # some error might happen here
  second_response = Mysql2::Client.new(:host => "localhost", :username => "root", etc...)
rescue => e
  all_errors << e
end

puts all_errors.inspect

经过快速搜索,我发现:(http://coderrr.wordpress.com/2008/11/07/the-simple-way-to-print-exceptions-in-ruby/)

# catch all exceptions (anything that derives from Exception)
begin
  ...
rescue Exception
  puts $!, $@
end

您可以使用at_exit处理程序,它可以访问$!中的最后一个异常。

喜欢

 at_exit {
   puts "the exception that killed us is", $! 
 }

如果你想在发生异常的时候捕获异常(而不是被捕获后),你可以使用ruby的“调试模式”(在控制台出现消息时输出消息)或者ruby-debug看看有没有办法在异常情况下启动Ruby调试器?


只需将所有代码包装在:

begin

   #yourcode
   #as much as you want
rescue 

end 
链接地址: http://www.djcxy.com/p/25875.html

上一篇: How to catch all exceptions as soon as they occur by just one rescue statement

下一篇: Timeout::Error isn't rescuing in Ruby