throw都用在catch语句中吗?正常应该怎样用呢,请大侠举个例子吧

解决方案 »

  1.   

    在我这段代码中,throw e后,打印的Caught inside demoproc.,还是先打印的Caught inside demoproc.呢
      

  2.   

    程序没有正常中止,抛出NullPointerException后退出了。
    catch (ArrayIndexOutOfBoundsException f){
    System.out.println("Recaught: " + f);
    }没有用。
      

  3.   

    throw e;你在这里throw以后,那么哪个方法或是类调用demoproc()出现Exception后,就由谁来处理。就是谁调用,谁处理。如果你不throw,那么就是自己处理。
      

  4.   

    在下愚昧,程序的执行过程是不是这样的呢?
    开始执行主程序,main()是入口,然后执行方法demoproc,这时遇到throw new NullPointerException();这时异常被引发,找到匹配的catch语句,catch(NullPointerException e){
    System.out.println("Caught inside demoproc.");
    throw e;
    },这时打印出"Caught inside demoproc.",throw e后,是怎样执行的呢?
      

  5.   

    throw e后,程序退回demoproc(),执行catch(因为你在执行demoproc后跑出了异常 e),但你跑出的是NullPointerException,而你捕获的是ArrayIndexOutOfBoundsException,所以程序终止,出现不是程序控制的窗口