java 异常处理 Exception


public class TestException {
 public TestException() {
 }

 //boolean testEx() throws Exception {
 boolean testEx() {
  boolean ret = true;
  try {
   ret = testEx1();
  } catch (Exception e) {
   System.out.println("testEx, catch exception");
   ret = false;
   throw e;
  }
  //finally 又执行了一次:有没有异常都执行
  finally {
   System.out.println("testEx, finally; return value=" + ret);
   return ret;
  }
   //return ret;
 }

 //boolean testEx1() throws Exception {
 boolean testEx1() {
  boolean ret = true;
//  try {
   ret = testEx2();
   if (!ret) {
    return false;
   }
   System.out.println("testEx1, at the end of try");
   return ret;
//  } catch (Exception e) {
//   System.out.println("testEx1, catch exception");
//   ret = false;
//   throw e;
//  } finally {
//   System.out.println("testEx1, finally; return value=" + ret);
//   return ret;
//  }
 }

 //boolean testEx2() throws Exception {
 boolean testEx2() {
  boolean ret = true;
  try {
   int b = 12;
   int c;
   for (int i = 2; i >= -2; i--) {
    c = b / i;
    System.out.println("i=" + i);
   }
   return true;
  } catch (Exception e) {
   System.out.println("testEx2, catch exception");
   ret = false;
   throw e;
  } finally {
   System.out.println("testEx2, finally; return value=" + ret);
   return ret;
  }
 }

 public static void main(String[] args) {
  TestException testException1 = new TestException();
  //try {
   boolean b=testException1.testEx();
   System.out.println(b);
//  } catch (Exception e) {
//   e.printStackTrace();
//  }
 }
}

猜你喜欢

转载自zw7534313.iteye.com/blog/2221895