有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVA中的异常理解throw关键字

在JAVA中进行异常处理练习时,我对各种事情感到有点困惑。基本上我不理解的是当遇到异常时程序的流程是如何的。我想了解在下面的场景中,程序的实际流程是如何发生的,以及我对这些概念的理解是正确的还是错误的

 public void myFunction(){

     try{

            //Some code......

        }catch(Exception e1){

            //If this Exception is occured handle it here.

        }catch(Exception e2){

           //if this exception has occured then

          throw new myException("whatever message required");
        }

       finally{

           //code that has to be executed 
       }

 }

现在我的理解是:

一,。如果没有发生异常,那么代码将平稳运行,最终执行finally块中的代码 2.若异常e1发生,则在第一个catch块中捕获它,并在该块中进行适当处理,然后执行最后一个块。 3.但如果发生异常e2会发生什么情况。在catch块中,我们抛出一个新的异常。 所以我调用myFunction的方法应该提供一些机制来处理这个问题 我的异常?因此,执行将转移到调用方法的catch块。正当 那么myFunction()的“finally”块会发生什么情况呢?那就不执行了? 程序流程是如何发生的?我真的很难发现当我们使用“扔”时会发生什么。当我们使用它时会发生什么


共 (4) 个答案

  1. # 1 楼答案

    Java Language Specification, section 11.3中可以找到您想要知道的关于Java在运行时如何处理异常的所有信息

    本质上,当遇到异常时,程序流停止,控制向上转移到调用堆栈,直到异常被try/catch捕获,或者直到异常到达堆栈底部,此时线程终止

    换句话说,“发生了什么,停止你正在做的任何事情,转到与此异常匹配的最近的catch块,除非不存在catch块,在这种情况下杀死线程”

    在您的情况下,e2将永远不会发生,因为所有异常都已被第一个catch块捕获。如果您想以不同的方式处理不同的异常,通常会使用多个catch块,例如,如果您正在解析文件中的数字,则在一个块中捕获IOException,在另一个块中捕获NumberFormatException

    您不必处理运行时异常(扩展RuntimeException的异常)。您必须显式地处理检查的异常(扩展Exception但不扩展RuntimeException的异常)

    无论是否引发异常,finally块中的代码都会被执行

  2. # 2 楼答案

    “调用myFunction的方法应该提供一些机制来处理此myException?” 应该是这样的,但是编译器只有在myException扩展了checked异常时才会让您这样做。 无论发生什么,最终块都将被执行

  3. # 3 楼答案

    请参见(您的修改的示例):

    try {
      // Some code ...
    }
    catch(SomeException e1) {
      // SomeException code ...
    }
    catch(SomeOtherException e2) { // SomeOtherException is NOT SomeException extension
      throw new myException("whatever message required");
    }
    finally {
      // Some final code ...
    }
    

    执行可能性:

     1. No exception at all:
       Some code executed
       Some final code executed
     2. SomeException is thrown:
       Some code (may be partially) executed  
       SomeException code executed
       Some final code executed
     3. SomeOtherException is thrown:
       Some code (may be partially) executed
       Some final code executed
       throw new myException("whatever message required");
     4. SomeStrangeException is thrown 
       Some code (may be partially) executed 
       Some final code executed
       System'll look for other try {} catch {} block to catch SomeStrangeException 
    
  4. # 4 楼答案

    考虑下面的示例(BaseCaly您的示例中只填充了一些真正的代码,它被设计为抛出一个Null PoExtExchange):

    public class Exceptions {
    
        public static void myMethod() {
            try{
                String s = null;
                s.length();
            }
            catch(NumberFormatException e1){
                System.out.println("Something unexpected happend");
            }
            catch(NullPointerException e2){
                throw new RuntimeException("Exactly what we want happened");
            }
            finally{
                System.out.println("Finally Block");
            }
        }
    
        public static void main(String[] args){
            try{
                myMethod();
            }
            catch(RuntimeException e){
                e.printStackTrace();
            }
        }
    }
    

    其输出为:

    Finally Block
    java.lang.RuntimeException: Exactly what we want happened
        at Exceptions.myMethod(Exceptions.java:14)
        at Exceptions.main(Exceptions.java:23)
    

    您可以看到,finally块是在新的RuntimeException被传递给调用方法之前执行的。否则,“Finally Block”输出将位于RuntimeException的stacktrace之后