1

我确定我在这里做了一些愚蠢的事情。我正在使用 Ruby 1.9.2-p312、jruby 1.6.8 并将 --1.9 选项设置为默认值。这是我第一次尝试挽救 SQLException。我故意通过拔下以太网连接使这段代码失败。

require 'java'
require 'lib/jt400-6.1'
require 'date'
require 'bigdecimal'

java_import 'com.ibm.as400.access.AS400JDBCDriver'def initialize(parms={})

class DB
  def initialize(parms = {})
    begin
      @connection ||= java.sql.DriverManager.get_connection "jdbc:as400://System/",parms[:user], parms[:pass]
    rescue java.sql.SQLException
      puts "Error connecting to iSeries. Error code: #{java.sql.SQLException.getErrorCode()}
                SQL State: #{java.sql.SQLException.getSQLState()}."
    end
  end
end

这会产生以下错误:

NoMethodError - undefined method `getErrorCode' for Java::JavaSql::SQLException:Class:
/home/nick/Projects/OrderBridge/app/db.rb:17:in `initialize'
/home/nick/Projects/OrderBridge/app/order-processor.rb:208:in `prepare'
/home/nick/Projects/OrderBridge/app/orderbridge.rb:43:in `POST /advanced'
org/jruby/RubyMethod.java:117:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:1212:in `compile!'
org/jruby/RubyProc.java:258:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:785:in `route!'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:801:in `route_eval'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:785:in `route!'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:822:in `process_route'
org/jruby/RubyKernel.java:1226:in `catch'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:820:in `process_route'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:784:in `route!'
org/jruby/RubyArray.java:1620:in `each'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:783:in `route!'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:886:in `dispatch!'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:719:in `call!'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:871:in `invoke'
org/jruby/RubyKernel.java:1226:in `catch'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:871:in `invoke'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:719:in `call!'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:705:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/path_traversal.rb:16:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/json_csrf.rb:17:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/nulllogger.rb:9:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/head.rb:9:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/showexceptions.rb:21:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/lint.rb:48:in `_call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/lint.rb:36:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/showexceptions.rb:24:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/commonlogger.rb:20:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/chunked.rb:43:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/content_length.rb:14:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/handler/webrick.rb:59:in `service'
/home/nick/.rvm/rubies/jruby-1.6.8/lib/ruby/1.9/webrick/httpserver.rb:111:in `service'
/home/nick/.rvm/rubies/jruby-1.6.8/lib/ruby/1.9/webrick/httpserver.rb:70:in `run'
/home/nick/.rvm/rubies/jruby-1.6.8/lib/ruby/1.9/webrick/server.rb:183:in `start_thread'
4

3 回答 3

2

您想将异常捕获到变量中。

class DB
  def initialize(parms = {})
    begin
      @connection ||= java.sql.DriverManager.get_connection "jdbc:as400://System/",parms[:user], parms[:pass]
    rescue java.sql.SQLException => e
      puts "Error connecting to iSeries. Error code: #{e.getErrorCode()}
                SQL State: #{e.getSQLState()}."
    end
  end
end

这样你就不会调用.getErrorCode()异常的类,而是调用异常的实例。

处理 jRuby 中的异常

于 2012-10-23T14:16:56.800 回答
0

您的问题在 puts 语句中。您指的是异常类而不是对象,请尝试

rescue java.sql.SQLException => e
  puts "Error connecting to iSeries. Error code: #{e.getErrorCode()}
            SQL State: #{e.getSQLState()}."
end
于 2012-10-23T14:17:05.637 回答
0

我遇到了这个类似的问题,并想分享我的解决方案,即使用“消息”和“wrapped_exception.error_code”,如果需要也可以使用。

class DB
  def initialize(parms = {})
    begin
      @connection ||= java.sql.DriverManager.get_connection "jdbc:as400://System/",parms[:user], parms[:pass]
    rescue java.sql.SQLException => e
      puts "Error message: #{e.message}
                Msg: #{e.wrapped_exception.error_code}."
    end
  end
end
于 2018-06-21T04:22:57.110 回答