這個(gè)代碼完全一摸一樣,為什么代碼finally后面的syso不執(zhí)行了
package com.imooc.test;
import javax.naming.spi.DirStateFactory.Result;
public class TryCatchTest {
? ? public static void main(String[] args) {
? ? ? ? // TODO Auto-generated method stub
? ? ? ? TryCatchTest tct? = new TryCatchTest();
? ? ? ? int result = tct.test();
? ? ? ? System.out.println("test()方法執(zhí)行完畢!返回值為:"+result);
? ? ? ??
? ? ? ? int result2 = tct.test();//tct是TryCatchTest類的新建實(shí)例化對(duì)象
? ? ? ? System.out.println("我想大聲告訴你,test2()方法執(zhí)行完畢!返回值為:");
? ? ??
? ? }
? ? public int test(){
? ? ? ? int divider = 10;
? ? ? ? int result = 100;
? ? ? ? try {
? ? ? ? ?while (divider > -1) {
? ? ? ? ? ? ?divider--;
? ? ? ? ? ? ?result =result + 100/divider;
? ? ? ? } return result;?
? ? ? ? } catch (Exception e) {
? ? ? ? ? e.printStackTrace();//
? ? ? ? ? System.out.println("循環(huán)拋出異常了!");?
? ? ? ? ? return-1;
? ? ? ? }
? ? ? ??
? ? }
? ? public int test2(){
? ? ? ? int divider = 10;
? ? ? ? int result = 100;
? ? ? ? try {
? ? ? ? ?while (divider > -1) {
? ? ? ? ? ? ?divider--;
? ? ? ? ? ? ?result =result + 100/divider;
? ? ? ? } return result;?
? ? ? ? } catch (Exception e) {
? ? ? ? ? e.printStackTrace();//
? ? ? ? ? System.out.println("循環(huán)拋出異常了!");?
? ? ? ? ? return result=999;
? ? ? ? }finally {
? ? ? ? ? ?
? ? ? ? ? ? System.out.println("這是finally,哈哈");
? ? ? ? ? ? System.out.println("我是result,我的值是"+result);
? ? ? ? }
? ? }
}
2020-04-28
你函數(shù)調(diào)用寫錯(cuò)了,main方法的第二個(gè)調(diào)用應(yīng)該是test2()。