著作一覧 |
すべての道はアマゾンへ通ずとか思ったり。
// ErrorHandling.java public class ErrorHandling { public interface Handler { void execute(); } public static void handle(Handler h) { try { h.execute(); } catch (Exception e) { System.out.println(e.getMessage()); } } }
//Handler.java public class Handler { public static void main(String[] args) { ErrorHandling.handle(new ErrorHandling.Handler() { public void execute() { int result = 10 / 2; System.out.println("result=" + result); } }); ErrorHandling.handle(new ErrorHandling.Handler() { public void execute() { int result = 10 / 0; System.out.println("result=" + result); } }); } }
C:\Home\test\eh>javac *.java C:\Home\test\eh>java -cp . Handler result=5 / by zero C:\Home\test\eh>
ジェズイットを見習え |