04-异常处理-动手动脑
1.請(qǐng)閱讀并運(yùn)行AboutException.java示例,然后通過(guò)后面的幾頁(yè)P(yáng)PT了解Java中實(shí)現(xiàn)異常處理的基礎(chǔ)知識(shí)。
import javax.swing.*;class AboutException {public static void main(String[] a) {int i=1, j=0, k;k=i/j;try{k = i/j; // Causes division-by-zero exception//throw new Exception("Hello.Exception!"); }catch ( ArithmeticException e){System.out.println("被0除. "+ e.getMessage());}catch (Exception e){if (e instanceof ArithmeticException)System.out.println("被0除");else{ System.out.println(e.getMessage());}}finally{JOptionPane.showConfirmDialog(null,"OK");}} }其輸出以下結(jié)果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ketangceshi.AboutException.main(AboutException.java:9)
有關(guān)異常處理的知識(shí):
處理機(jī)制:吧可能發(fā)生錯(cuò)誤的代碼放入try語(yǔ)句塊中,當(dāng)程序檢測(cè)到出現(xiàn)了一個(gè)錯(cuò)誤時(shí)會(huì)拋出一個(gè)異常對(duì)象。異常處理代碼會(huì)捕獲并處理這個(gè)錯(cuò)誤(catch語(yǔ)句塊中的代碼用于處理錯(cuò)誤)。當(dāng)異常發(fā)生時(shí),程序控制流程由try語(yǔ)句塊跳轉(zhuǎn)到catch語(yǔ)句塊。不管是否有異常發(fā)生,finally語(yǔ)句塊中的語(yǔ)句始終保持被執(zhí)行。如果沒(méi)有提供合適的異常處理代碼,JVM將會(huì)結(jié)束掉整個(gè)應(yīng)用程序。
異常分類:Throwable類有兩個(gè)直接子類: Exception:出現(xiàn)的問(wèn)題是可以被捕獲的; Error:系統(tǒng)錯(cuò)誤,通常由JVM處理。 可捕獲的異常又可以分為兩類: (1)Check異常:直接派生自Exception的異常類,必須被捕獲或再次聲明拋出 (2)Runtime異常:派生自RuntimeException的異常類。使用throw語(yǔ)句可以隨時(shí)拋出這種異常對(duì)象: throw new ArithmeticException(…);
上述情況出現(xiàn)的原因:異常處理有“多態(tài)”的特性。
異常多態(tài)特性
可以有多個(gè)catch語(yǔ)句塊,每個(gè)代碼塊捕獲一種異常。在某個(gè)try塊后有兩個(gè)不同的catch 塊捕獲兩個(gè)相同類型的異常是語(yǔ)法錯(cuò)誤。 使用catch語(yǔ)句,只能捕獲Exception類及其子類的對(duì)象。因此,一個(gè)捕獲Exception對(duì)象的catch語(yǔ)句塊可以捕獲所有“可捕獲”的異常。 將catch(Exception e)放在別的catch塊前面會(huì)使這些catch塊都不執(zhí)行,因此Java不會(huì)編譯這個(gè)程序。
finally的功能
資源泄露:當(dāng)一個(gè)資源不再被某應(yīng)用程序使用,但此程序并未向系統(tǒng)聲明不再使用此資源時(shí)發(fā)生這種情況 finally語(yǔ)句塊主要用于解決資源泄露問(wèn)題,它位于catch語(yǔ)句塊之后,JVM保證它們一定執(zhí)行。 注意:finally語(yǔ)句塊中也可能發(fā)生異常,如果這種情況發(fā)生,先前的異常被放棄。
2.多層的異常捕獲-1
public class CatchWho { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/內(nèi)層try-catch"); }throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("發(fā)生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); } } }運(yùn)行結(jié)果如下:
3.多層的異常捕獲-2
public class CatchWho2 { public static void main(String[] args) { try {try { throw new ArrayIndexOutOfBoundsException(); } catch(ArithmeticException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/內(nèi)層try-catch"); }throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("發(fā)生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); } } }運(yùn)行結(jié)果如下:
?
4.動(dòng)手動(dòng)腦:當(dāng)有多個(gè)嵌套的try...catch...finally時(shí),要特別注意finally的執(zhí)行時(shí)機(jī)。
public class EmbededFinally {public static void main(String args[]) {int result;try {System.out.println("in Level 1");try {System.out.println("in Level 2");// result=100/0; //Level 2try {System.out.println("in Level 3");result=100/0; //Level 3 } catch (Exception e) {System.out.println("Level 3:" + e.getClass().toString());}finally {System.out.println("In Level 3 finally");}// result=100/0; //Level 2 }catch (Exception e) {System.out.println("Level 2:" + e.getClass().toString());}finally {System.out.println("In Level 2 finally");}// result = 100 / 0; //level 1 } catch (Exception e) {System.out.println("Level 1:" + e.getClass().toString());}finally {. System.out.println("In Level 1 finally");}}}運(yùn)行結(jié)果如下:
當(dāng)有多層嵌套的finally時(shí),異常在不同的層次拋出,在不同的位置拋出,可能會(huì)導(dǎo)致不同的finally語(yǔ)句塊執(zhí)行順序。
5.finally語(yǔ)句塊一定會(huì)執(zhí)行嗎?
public class SystemExitAndFinally {public static void main(String[] args){try{System.out.println("in main");throw new Exception("Exception is thrown in main");//System.exit(0); }catch(Exception e){System.out.println(e.getMessage());System.exit(0);}finally{System.out.println("in finally");}}}運(yùn)行結(jié)果如下:
由運(yùn)行結(jié)果分析得,finally語(yǔ)句塊中得內(nèi)容不一定會(huì)執(zhí)行。
6.如何跟蹤異常的傳播路徑?
// UsingExceptions.java // Demonstrating the getMessage and printStackTrace // methods inherited into all exception classes. public class PrintExceptionStack {public static void main( String args[] ){try {method1();}catch ( Exception e ) {System.err.println( e.getMessage() + "\n" );e.printStackTrace();}}public static void method1() throws Exception{method2();}public static void method2() throws Exception{method3();}public static void method3() throws Exception{throw new Exception( "Exception thrown in method3" );} }運(yùn)行結(jié)果如下:
當(dāng)程序中出現(xiàn)異常時(shí),JVM會(huì)依據(jù)方法調(diào)用順序依次查找有關(guān)的錯(cuò)誤處理程序。 可使用printStackTrace 和 getMessage方法了解異常發(fā)生的情況: printStackTrace:打印方法調(diào)用堆棧。 每個(gè)Throwable類的對(duì)象都有一個(gè)getMessage方法,它返回一個(gè)字串,這個(gè)字串是在Exception構(gòu)造函數(shù)中傳入的,通常讓這一字串包含特定異常的相關(guān)信息。
轉(zhuǎn)載于:https://www.cnblogs.com/Qi77/p/9941241.html
總結(jié)
以上是生活随笔為你收集整理的04-异常处理-动手动脑的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在本地生成ssh-key 免密码远程cl
- 下一篇: 算法练习-002-返回一个set数组