日期:2014-05-20  浏览次数:20709 次

新手遇到的问题,希望有大神来帮忙
//如果IOException发生的话,那么4是否输出?
public void fun()
{
int i;
try
{
i=System.in.read();
System.out.println("1");
}catch(IOException e)
{
System.out.println("2");
}
finally
{
System.out.println("3");
}
System.out.println("4");
}


------解决方案--------------------
因为用try catch捕获了IO异常,所以出现IO异常的话,会执行catch中的代码,而程序不会退出,所以会继续执行System.out.println("4");

但如果有非IO异常产生,那么程序会终止,也就不会执行到System.out.println("4");这句了。
楼主可以这样改测试一下:

Java code
public void fun() throws SQLException {
        int i;
        try {
            i = System.in.read();
            System.out.println("1");
            throw new SQLException("the SQL Exception");
        } catch (IOException e) {
            System.out.println("2");
        } finally {
            System.out.println("3");
        }
        System.out.println("4");
    }