Memo

例外のハンドリング Javaの例外もJRubyで扱うことが出来る。Javaの例外が発生するとJRubyのNativeException に変換されてraise ( = Javaのthrow ) される。 NativeExceptionはJavaの例外をラップしてくれる。従って rescue( = Javaのcatch ) でNativeExceptionを指定すれば、Javano例外を全て補足出来る。NativeExceptionのcauseメソッドを使用すると、Javano例外クラスを抽出して操作が可能である。 更に、Javaの例外クラスを指定してrescueすることもできる。java.lang.Exceptionを指定してもいいし、サブクラスを指定してもいい。例示する。 wc.rb
require ‘java’
include_class ‘java.io.FileReader’
include_class ‘java.io.Bufferedreader’ characters = 0
words = 0
lines = 0 begin
 f = BufferedReader.new(FileReader.new(ARGV[0]))
 while (L=f.readLine)
  lines+=1
  words+=L.split.length
  characters+=L.length
 end
 puts “#{lines} #{words} #{characters}”
 rescue java.io.FileNotFoundException ・・・ (1)
 puts “Filenot found: #{ARGV[0]}”
 cause java.io.IOException=>e
 puts “Error !! #{e}”
end

JRuby - WikiwikiDosanko - livedoor Wiki(ウィキ)
To Tumblr, Love PixelUnion

We're updating Fluid!

Soon, we'll be updating the look and feel of this theme. Read about the changes here. You can easily turn off this notification in the theme customization panel.

Close