package imooc_exception_demo; public class TryCatchText { public static void main(String[] args) { TryCatchText tc = new TryCatchText(); int result = tc.text(); int result2 = tc.text2(); System.out.println("方法执行完毕,返回值为:" + result); System.out.println("text2执行完毕"); } /* devider(除数)result(结果) try-catch捕获while循环 每次循环devider-1次,result+=100/devider 若有异常打印抛出异常了返回-1否则返回result */ public int text() { int devider = 10; int result = 100; try { while (devider >= -1) { devider--; result = result + 100 / devider; } return result; } catch (Exception e) { System.out.println("输出异常了"); return -1; } } /* devider(除数)result(结果) try-catch捕获while循环 每次循环devider-1次,result=999 若有异常打印抛出异常了返回-1否则返回result finally:这是finally!!,输出result */ public int text2() { int devider = 10; int result = 100; try { while (devider >= -1) { devider--; result = result + 100 / devider; } return result; } catch (Exception e) { System.out.println("输出异常了"); return result = 999; } } finally { System.out.println("这是finally!!"); System.out.println("我是result,我的值是:" +result ); } }
你应该把finally放进函数中,
在return result = 999;删掉一个“}”然后在最后加一个“}”
位置应该没关系,突然发现是finally前面多了一个“}”,?
try{}catch{}finally{}
这三者是同一层次的。你代码里面的finally位置有问题
没用结果还是一样
给catch加一句e.printStackTrace();试试