日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

异常处理的动手动脑

發布時間:2025/4/16 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 异常处理的动手动脑 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一,拋異常的類型

拋異常:1,Error錯誤,系統錯誤 2,Exception錯誤
Exception類又包括:RuntimeException非檢查異常和檢查異常。
Error例子: 1.AssertionError程序運行期間判斷某個條件是否滿足
,不滿足時,拋出AssertionError。

???????????????? 2.OOM Error系統內存不足。

二,小探索

int i=1, j=0, k;
k=i/j;


javac生成idiv字節碼指令
double d1=100,d2=0,result;
?? ?result=d1/d2;
System.out.println("浮點數除以零:" + data);

浮點數除以0:Infinity
javac生成ddiv字節碼指令,JVM在具體實現這兩個指令時,采用了不同的處理策略,導致兩段代碼運行時得到不同的結果。

三,try catch的嵌套

public class CatchWho { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); }throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("發生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); } } }
ArrayIndexOutOfBoundsException/內層try-catch 發生ArithmeticException

上述是結果

public class CatchWho2 { public static void main(String[] args) { try {try { throw new ArrayIndexOutOfBoundsException(); } catch(ArithmeticException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); }throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("發生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); } } } ArrayIndexOutOfBoundsException/外層try-catch

看下面這段代碼,

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");}}}

運行結果為

in Level 1 in Level 2 in Level 3 Level 3:class java.lang.ArithmeticException In Level 3 finally In Level 2 finally In Level 1 finally

當有多層嵌套的finally時,異常在不同的層次拋出?? ?,在不同的位置拋出,會導致不同的finally語句塊執行順序。

四,自定義異常

Class MyException extends Exception
?? ?{ …??? }
Class MyClass {
?? ??? ?void someMethod() {
?? ??? ??? ?if (條件) throw new MyException();
?? ??? ?}
?? ?}


轉載于:https://www.cnblogs.com/xcl666/p/9938913.html

總結

以上是生活随笔為你收集整理的异常处理的动手动脑的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。