catch里面的e.printStackTrace(); 到底有什么用 為什么要這樣寫 真不明白!
? ? 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;
? ? ?}
? ?
? ? ? }
2017-10-17
catch(Exception e){ e.printStackTrace() ; } 當(dāng)try語(yǔ)句中出現(xiàn)異常是時(shí),會(huì)執(zhí)行catch中的語(yǔ)句,java運(yùn)行時(shí)系統(tǒng)會(huì)自動(dòng)將catch括號(hào)中的Exception e 初始化,也就是實(shí)例化Exception類型的對(duì)象。e是此對(duì)象引用名稱。然后e(引用)會(huì)自動(dòng)調(diào)用Exception類中指定的方法,也就出現(xiàn)了e.printStackTrace() ;。 printStackTrace()方法的意思是:在命令行打印異常信息在程序中出錯(cuò)的位置及原因。(這是白話解釋,比較容易理解)
2018-05-22
大V俺的沙發(fā)不懂
2017-10-17
將此 throwable 及其追蹤輸出至標(biāo)準(zhǔn)錯(cuò)誤流。此方法將此 Throwable 對(duì)象的堆棧跟蹤輸出至錯(cuò)誤輸出流,作為字段 System.err 的值。輸出的第一行包含此對(duì)象的 toString() 方法的結(jié)果。剩余行表示以前由方法 fillInStackTrace() 記錄的數(shù)據(jù)。此信息的格式取決于實(shí)現(xiàn),但以下示例是最常見的: java.lang.NullPointerException
? ? ? ? at MyClass.mash(MyClass.java:9)
? ? ? ? at MyClass.crunch(MyClass.java:6)
? ? ? ? at MyClass.main(MyClass.java:3)