在Java中的适当catch之前最终阻止执行

时间:2018-07-22 06:08:47

标签: java exception-handling

我写了一个简单的try,使用java最终捕获catch,但令我感到困惑的是,有时finally最终在适当的catch块之前执行,而在finally块执行之后,适当的catch块正在执行。在其他时候,获取适当的catch块,然后最终执行该块,据我所知这是正确的。

下面是我执行的代码

//Div2.java
package exceptionHandling;

import java.util.Scanner;

class Div2 
{

    public static void main(String[] args) 
    {
        try
        {
            Scanner s = new Scanner(System.in);
            System.out.println("Please enter two values");
            int x1 = Integer.parseInt(s.nextLine());
            int x2 = Integer.parseInt(s.nextLine());
            int x3 = x1 / x2;
            System.out.println("Division in Div2 = "+x3);
        }
        catch(ArithmeticException ae)
        {
            System.err.println("dont enter second value as 0 as it is the denominator");
        }
        catch(NumberFormatException nfe)

        {
            System.err.println("please dont enter alphanumerical or string values or special symbols");
        }

        finally
        {
            System.out.println("I am from finally block");
        }

    }

}

输出如下 请输入两个值

10
0
I am from finally block
dont enter second value as 0 as it is the denominator

,其他时候的输出是

10
0
dont enter second value as 0 as it is the denominator
I am from finally block

0 个答案:

没有答案