LAPTOP-8KA88UT5_20200911-陈浩

问题

public class Test
{
    public static int aMethod(int i)throws Exception
    {
        try{
            return i / 10;
        }
        catch (Exception ex)
        {
            throw new Exception("exception in a Method");
        } finally{
            System.out.printf("finally");
        }
    }
 
    public static void main(String [] args)
    {
        try
        {
            aMethod(0);
        }
        catch (Exception ex)
        {
            System.out.printf("exception in main");
        }
        System.out.printf("finished");
    }
}

解决:

对于类方法aMethod( ), i / 10这一行代码 ; 无论i是多少,永远不会抛出异常,所以catch语句不会执行。

而finally语句是必定执行的语句。

在main方法中调用该类方法,因为0是int型,所以不会抛出异常,catch语句不会执行,故先输出 finally,

后输出finished, 故最后输出结果为: finally finished

吐槽

明天又要考试了,逢考必慌 !