public class TestException { public static void main(String[] args) {
try {
oneMethod();
} catch (Exception e) {
System.out.println("there is an exception");
} } static void oneMethod() {
try {
twoMethod();
System.out.println("normal");
} catch (ArithmeticException e) {
System.out.println("there is an arithmeticException");
} finally {
System.out.println("in finally");
}
System.out.println("outside try block"); } static void twoMethod() {
throw new NullPointerException();
}}这个程序的结果是:
in finally
there is an exception

我困惑的是,结果为什么不是
in finally
outside try block
there is an exception

请大家来看看,帮忙解释下

解决方案 »

  1.   

    因为在oneMethod的try里会收到由twoMethod()抛出的NullPointerException,
    但是catch块里并没有catch这类型的异常,所以方法oneMethod执行完finally里的语句
    后就会终止,并将异常往上层抛。
      

  2.   

    执行完finally里的语句后就会终止,并将异常往上层抛这句话中的将的”将异常往上层抛“,又没有throws,怎么向上抛啊
      

  3.   

    twoMethod()抛出的NullPointerException
    而没有定义子类catch的话程序就会抛向
     catch (Exception e) 
    然后做finally,下一步就终止了..不会去做 System.out.println("outside try block")
        
              
      

  4.   

    如果System.out.println("outside try block") ;
    在mian方法的最后一句的话是会输出outside try block的
      

  5.   

    刚好我也在看异常处理,修改了一下楼主的程序
    public static void main(String[] args) {
            try {
                oneMethod();
            } catch (Exception e) {
                System.out.println("there is an exception");
            }
            System.out.println("Exception has handled in OneMethod() fuction,");
            System.out.println("It's no need to hand again in the main fuction");

    }
    catch (ArithmeticException e) {
                System.out.println("there is an arithmeticException");
            }
            /*此处捕获NullPointException异常,经此处理System.out.println("outside try block");就会执行*/
            catch(NullPointerException ex){   
               System.out.println("this is a NullPointException");
            } 
            
            finally {
                System.out.println("in finally");
            }
    输出:
    this is a NullPointException
    in finally
    outside try block
    Exception has handled in OneMethod() fuction,
    It's no need to hand again in the main fuction
      

  6.   

    明白楼上的意思,public class TestException1 { public static void main(String[] args) {
    try {
    oneMethod();
    } catch (Exception e) {
    System.out.println("there is an exception");
    }
    System.out.println("Exception has handled in OneMethod() fuction,");
    System.out.println("It's no need to hand again in the main fuction");
    } static void oneMethod() {
    try {
    twoMethod();
    System.out.println("normal");
    } catch (ArithmeticException e) {
    System.out.println("there is an arithmeticException");
    catch (NullPointerException ex) {
    System.out.println("this is a NullPointException");
    finally {
    System.out.println("in finally");
    }
    System.out.println("outside try block"); } static void twoMethod() {
    throw new NullPointerException();
    }}
    的结果为:
    this is a NullPointException
    in finally
    outside try block
    Exception has handled in OneMethod() fuction,
    It's no need to hand again in the main fuction
    而当去掉上面带颜色的语句之后(即: catch (NullPointerException ex) {
    System.out.println("this is a NullPointException");

    结果为:
    in finally
    there is an exception
    Exception has handled in OneMethod() fuction,
    It's no need to hand again in the main fuction

    我不太明白的是去了带色的语句后为什么不打印outside try block????????
      

  7.   

    只要在System.out.println("outside try block") 之前发生了异常,程序就不会往下执行了,
    你应该debug一下,eclipse会清楚的告诉你程序每一部执行了什么。
      

  8.   

    但“System.out.println("outside try block") ”在try..catch..之外啊,执行完try..catch后,难道不会执行System.out.println("outside try block")?????????
      

  9.   

    是这样的:
    如果oneMethod这个方法中捕捉到了twoMethod抛出的NullPointerException,那么执行完finally后就会执行System.out.println("outside try block");
    但是这里oneMethod这个方法中没有捕获到twoMethod抛出的NullPointerException,那么执行完finally就会停止,因为他在try里面抛出了异常但是没有被捕捉到,那么就等于
    说这个try catch块没有用,所以当然跳出try catch快后就会停止(因为他会抛出个异常啊)。我把程序改成这样你就会明白了
    public class TestException {
    public static void main(String[] args) {
    //        try {
                oneMethod();
    //        } catch (Exception e) {
    //            System.out.println("there is an exception");
    //        }    }    static void oneMethod() {
            try {
                twoMethod();
                System.out.println("normal");
            } catch (ArithmeticException e) {
                System.out.println("there is an arithmeticException");
            } finally {
                System.out.println("in finally");
            }
            System.out.println("outside try block");    }    static void twoMethod() {
            throw new NullPointerException();
        }}这样的话
    in finally
    Exception in thread "main" java.lang.NullPointerException
    at Test12_27.TestException.twoMethod(TestException.java:27)
    at Test12_27.TestException.oneMethod(TestException.java:15)
    at Test12_27.TestException.main(TestException.java:6)
    这是输出。
    现在该明白了吧。
      

  10.   

    我想因为System.out.println("outside try block");这句在oneMethod()方法中,
    当发现异常后回回到mian方法中去
    当异常处理完毕后不会再回到oneMethod()方法了,而是继续执行mian方法
    所以就不再输出outside try block.
       不知道我理解的对不对,请达人指点!