当前位置: 代码迷 >> J2ME >> 新手遇到的有关问题,希望有大神来帮忙
  详细解决方案

新手遇到的有关问题,希望有大神来帮忙

热度:3262   发布时间:2013-02-25 21:31:35.0
新手遇到的问题,希望有大神来帮忙
//如果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");    }
  相关解决方案