Java: try, catch, finally
-Throwable (Java Platform SE 6) (Java 言語のすべてのエラーと例外のスーパークラス)
-Exception (Java Platform SE 6)
-RuntimeException (Java Platform SE 6)

Ruby: begin, rescue, else, ensure
-Ruby 1.8.7 リファレンスマニュアル > 制御構造
-class Exception (全ての例外の祖先のクラス)
-class StandardError
-class RuntimeError

実行環境はMacBook Air.


$ uname -m -r -s -v
Darwin 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386

Javaの例外処理サンプルコードと実行結果。


$ java -version
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07-334-10M3326)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02-334, mixed mode)
 
$ cat ./Reigai.java
public class Reigai{
 
  public static void main(String[] args){
 
    try{
      System.err.println("{{{Exception}}}");
      reigai("Exception");
    }catch(Exception e){
      System.err.println("reigai=" + e);
    }
    System.err.println("");
 
    try{
      System.err.println("{{{RuntimeException}}}");
      reigai("RuntimeException");
    }catch(Exception e){
      System.err.println("reigai=" + e);
    }
    System.err.println("");
 
    try{
      System.err.println("{{{null}}}");
      reigai(null);
    }catch(Exception e){
      System.err.println("reigai=" + e);
    }
    System.err.println("");
  }
 
  public static void throwReigai(String msg) throws Exception {
    if("RuntimeException".equals(msg)){
      System.err.println("[RuntimeException]");
      throw new RuntimeException("Hello RuntimeException");
    }else if("Exception".equals(msg)){
      System.err.println("[Exception]");
      throw new Exception("Hello Exception");
    }
  }
 
  public static void reigai(String msg) throws Exception {
    try{
      System.err.println("[throwReigai]");
      throwReigai(msg);
    }catch(RuntimeException e){
      System.err.println("[catch RuntimeException]");
      System.err.println(e.getMessage());
      e.printStackTrace();
      throw e;
    }catch(Exception e){
      System.err.println("[catch Exception]");
      System.err.println(e.getMessage());
      e.printStackTrace();
      throw e;
    }finally{
      System.err.println("[finally]");
    }
  }
}
 
$ javac ./Reigai.java
 
$ java Reigai
{{{Exception}}}
[throwReigai]
[Exception]
[catch Exception]
Hello Exception
java.lang.Exception: Hello Exception
    at Reigai.throwReigai(Reigai.java:36)
    at Reigai.reigai(Reigai.java:43)
    at Reigai.main(Reigai.java:7)
[finally]
reigai=java.lang.Exception: Hello Exception
 
{{{RuntimeException}}}
[throwReigai]
[RuntimeException]
[catch RuntimeException]
Hello RuntimeException
java.lang.RuntimeException: Hello RuntimeException
    at Reigai.throwReigai(Reigai.java:33)
    at Reigai.reigai(Reigai.java:43)
    at Reigai.main(Reigai.java:15)
[finally]
reigai=java.lang.RuntimeException: Hello RuntimeException
 
{{{null}}}
[throwReigai]
[finally]

Rubyの例外処理サンプルコードと実行結果。


$ ruby -v
ruby 1.8.7 (2010-12-23 patchlevel 330) [i686-darwin10]
 
$ cat ./reigai.rb 
def reigai_raise(msg)
  if msg == 'StandardError'
    $stderr.puts '[StandardError]'
    raise StandardError.new('Hello Standard Error!')
  elsif msg == 'RuntimeError'
    $stderr.puts '[RuntimeError]'
    raise RuntimeError.new('Hello Runtime Error!')
  end
end
 
def reigai(msg)
  begin
    $stderr.puts '[reigai_raise]'
    reigai_raise(msg)
  rescue RuntimeError => e
    $stderr.puts '[rescue RuntimeError]'
    $stderr.puts e.to_s
    $stderr.puts e.backtrace.inspect
    raise e
  rescue
    $stderr.puts '[rescue]'
    $stderr.puts $!.to_s
    $stderr.puts $!.backtrace.inspect
    raise $!
  else
    $stderr.puts '[else]'
  ensure
    $stderr.puts '[ensure]'
  end
end
 
$stderr.puts '{{{StandardError}}}'
reigai('StandardError') rescue $stderr.puts "reigai=#{$!.inspect}"
$stderr.puts ''
 
$stderr.puts '{{{RuntimeError}}}'
reigai('RuntimeError') rescue $stderr.puts "reigai=#{$!.inspect}"
$stderr.puts ''
 
$stderr.puts '{{{nil}}}'
reigai(nil) rescue $stderr.puts "reigai=#{$!.inspect}"
$stderr.puts ''
 
$ ruby ./reigai.rb 
{{{StandardError}}}
[reigai_raise]
[StandardError]
[rescue]
Hello Standard Error!
["./reigai.rb:4:in `reigai_raise'", "./reigai.rb:14:in `reigai'", "./reigai.rb:33"]
[ensure]
reigai=#<StandardError: Hello Standard Error!>
 
{{{RuntimeError}}}
[reigai_raise]
[RuntimeError]
[rescue RuntimeError]
Hello Runtime Error!
["./reigai.rb:7:in `reigai_raise'", "./reigai.rb:14:in `reigai'", "./reigai.rb:37"]
[ensure]
reigai=#<RuntimeError: Hello Runtime Error!>
 
{{{nil}}}
[reigai_raise]
[else]
[ensure]

tags: java ruby

Posted by NI-Lab. (@nilab)