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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 异常处理(标准抛异常、异常处理、多异常、Finally、多线程异常处理、获取异常的堆栈信息、链试异常、自定义异常)

發布時間:2025/3/15 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 异常处理(标准抛异常、异常处理、多异常、Finally、多线程异常处理、获取异常的堆栈信息、链试异常、自定义异常) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用 catch 處理異常(標準拋異常)

public class Main {public static void main (String args[]) {int array[]={20,20,40};int num1=15,num2=10;int result=10;try{result = num1/num2;System.out.println("結果為 " +result);for(int i =5;i >=0; i--) {System.out.println ("數組的元素值為 " +array[i]);}}catch (Exception e) {System.out.println("觸發異常 : "+e);}} }

以上代碼運行輸出結果為:

結果為 1 觸發異常 : java.lang.ArrayIndexOutOfBoundsException: 5

?

異常處理

class ExceptionDemo {public static void main(String[] args) {try {throw new Exception("My Exception");} catch (Exception e) {System.err.println("Caught Exception");System.err.println("getMessage():" + e.getMessage());System.err.println("getLocalizedMessage():" + e.getLocalizedMessage());System.err.println("toString():" + e);System.err.println("printStackTrace():");e.printStackTrace();}} }

以上代碼運行輸出結果為:

Caught Exception getMessage():My Exception getLocalizedMessage():My Exception toString():java.lang.Exception: My Exception printStackTrace(): java.lang.Exception: My Exceptionat ExceptionDemo.main(ExceptionDemo.java:5)

?

多個異常處理(多個catch)

class Demo { int div(int a,int b) throws ArithmeticException,ArrayIndexOutOfBoundsException//在功能上通過throws的關鍵字聲明該功能可能出現問題 { int []arr = new int [a]; System.out.println(arr[4]);//制造的第一處異常 return a/b;//制造的第二處異常 } } class ExceptionDemo { public static void main(String[]args) //throws Exception { Demo d = new Demo(); try { int x = d.div(4,0);//程序運行截圖中的三組示例 分別對應此處的三行代碼 //int x = d.div(5,0); //int x = d.div(4,1); System.out.println("x="+x); } catch (ArithmeticException e) { System.out.println(e.toString()); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(e.toString()); } catch (Exception e)//父類 寫在此處是為了捕捉其他沒預料到的異常 只能寫在子類異常的代碼后面 //不過一般情況下是不寫的 { System.out.println(e.toString()); } System.out.println("Over"); } }

以上代碼運行輸出結果為:

java.lang.ArrayIndexOutOfBoundsException: 4 Over

?

Finally的用法

Java 中的 Finally 關鍵一般與try一起使用,在程序進入try塊之后,無論程序是因為異常而中止或其它方式返回終止的,finally塊的內容一定會被執行 。?

public class ExceptionDemo2 {public static void main(String[] argv) {new ExceptionDemo2().doTheWork();}public void doTheWork() {Object o = null;for (int i=0; i<5; i++) {try {o = makeObj(i);}catch (IllegalArgumentException e) {System.err.println("Error: ("+ e.getMessage()+").");return; }finally {System.err.println("都已執行完畢");if (o==null)System.exit(0);}System.out.println(o); }}public Object makeObj(int type) throws IllegalArgumentException {if (type == 1) throw new IllegalArgumentException("不是指定的類型: " + type);return new Object();} }

以上代碼運行輸出結果為:

都已執行完畢 java.lang.Object@7852e922 Error: (不是指定的類型:1). 都已執行完畢

?

多線程異常處理

class MyThread extends Thread{public void run(){System.out.println("Throwing in " +"MyThread");throw new RuntimeException();} } class Main {public static void main(String[] args){MyThread t = new MyThread();t.start();try{Thread.sleep(1000);}catch (Exception x){System.out.println("Caught it" + x);}System.out.println("Exiting main");} }

以上代碼運行輸出結果為:

Throwing in MyThread Exception in thread "Thread-0" java.lang.RuntimeExceptionat testapp.MyThread.run(Main.java:19) Exiting main

?

獲取異常的堆棧信息

public class Main{public static void main (String args[]){int array[]={20,20,40};int num1=15,num2=10;int result=10;try{result = num1/num2;System.out.println("The result is" +result);for(int i =5; i>=0; i--) {System.out.println("The value of array is" +array[i]);}}catch (Exception e) {e.printStackTrace();}} }

以上代碼運行輸出結果為:

The result is1 java.lang.ArrayIndexOutOfBoundsException: 5at testapp.Main.main(Main.java:28)

?

重載方法異常處理

public class Main {double method(int i) throws Exception{return i/0;}boolean method(boolean b) {return !b;}static double method(int x, double y) throws Exception {return x + y ;}static double method(double x, double y) {return x + y - 3;} public static void main(String[] args) {Main mn = new Main();try{System.out.println(method(10, 20.0));System.out.println(method(10.0, 20));System.out.println(method(10.0, 20.0));System.out.println(mn.method(10));}catch (Exception ex){System.out.println("exception occoure: "+ ex);}} }

以上代碼運行輸出結果為:

30.0 27.0 27.0 exception occoure: java.lang.ArithmeticException: / by zero

?

鏈試異常

public class Main {public static void main (String args[])throws Exception {int n=20,result=0;try{result=n/0;System.out.println("結果為"+result);}catch(ArithmeticException ex){System.out.println("發算術異常: "+ex);try {throw new NumberFormatException();}catch(NumberFormatException ex1) {System.out.println("手動拋出鏈試異常 : "+ex1);}}} }

以上代碼運行輸出結果為:

發算術異常: java.lang.ArithmeticException: / by zero 手動拋出鏈試異常 : java.lang.NumberFormatException

?

自定義異常

class WrongInputException extends Exception { // 自定義的類WrongInputException(String s) {super(s);} } class Input {void method() throws WrongInputException {throw new WrongInputException("Wrong input"); // 拋出自定義的類} } class TestInput {public static void main(String[] args){try {new Input().method();}catch(WrongInputException wie) {System.out.println(wie.getMessage());}} }

以上代碼運行輸出結果為:

Wrong input

?

總結

以上是生活随笔為你收集整理的Java 异常处理(标准抛异常、异常处理、多异常、Finally、多线程异常处理、获取异常的堆栈信息、链试异常、自定义异常)的全部內容,希望文章能夠幫你解決所遇到的問題。

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