Java異常類型及處理詳情
異常結(jié)構(gòu)為:
Throwable
為頂級父類
- 子類
Error
為嚴(yán)重報(bào)錯 , - 子類
Exception
就是我們所說的異常了
一、異常處理的關(guān)鍵字
java
中處理異常的有五個關(guān)鍵字: try
、catch
、finally
、 throw
、throws
throw
拋出異常 , thorws
聲明異常 , 捕獲異常 try_catch
1、throw
public class SegmentFault { public static void main(String[] args) { /** * throw 拋出異常 * 格式 - throw new 異常類名(參數(shù)); * */ // 創(chuàng)建一個數(shù)組 int [] arr = { 2, 4, 56 ,5}; // 根據(jù)索引找到對應(yīng)的元素 int index = 4; int element = getElement(arr,index); System.out.println(element); System.out.println("owo"); // 運(yùn)行錯誤 無法繼續(xù) } /** throw 拋出異常 提醒你必須處理 */ public static int getElement(int [] arr, int index){ // 判斷數(shù)組索引是否越界 if (index < 0 || index > arr.length -1){ /** * 條件滿足越界 當(dāng)執(zhí)行到throw拋出異常后就無法運(yùn)行,結(jié)束方法并且提示 * */ throw new ArrayIndexOutOfBoundsException("數(shù)組下標(biāo)越界異常"); } int element = arr[index]; return element; } }
異常結(jié)果為:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 數(shù)組下標(biāo)越界異常
2、throws
public class SegmentFault{ public static void main(String [] args){ read("a.txt"); } public static void read(String path) throws FileNotFoundException, IOException { if (!path.equals("a.txt")){ // 如果沒有a.txt // 如果不是 a.txt 該文件不存在 是一個錯誤 也就是異常 throw throw new FileNotFoundException("文件不存在"); } if (!path.equals("b.txt")){ throw new IOException("文件不存在"); } } }
異常結(jié)果為:
Exception in thread "main" java.io.IOException: 文件不存在
try
、catch
、finally
+ Throwable
中的常用方法。
Throwable
常用方法如下:
printStackTrace()
: *打印異常詳細(xì)信息。getMessage()
: 獲取異常原因。toString():
獲取異常類型及描述信息。
public class Demo03 { public static void main(String[] args) { /** * try- catch 捕獲異常 * */ // 可能會生成的異常 try { // 捕獲或者聲明 read("b.txt"); } catch (FileNotFoundException e) { // 使用某種捕獲,實(shí)現(xiàn)對異常的處理 System.out.println(e); /** * Throwable中的查看方法 * getMessage 獲取異常信息 提示給用戶看的 * toString 獲取異常的類型和異常描述(不用) * printStackTrace * */ System.out.println("Throwable常用方法測試"); System.out.println(e.getMessage()); // 文件不存在 System.out.println(e.toString()); e.printStackTrace(); } finally { System.out.println("不管程序怎樣,這里都會被執(zhí)行"); } System.out.println("over"); } public static void read(String path) throws FileNotFoundException { if (!path.equals("a.txt")) { throw new FileNotFoundException("文件不存在"); } } }
輸出結(jié)果為:
java.io.FileNotFoundException: 文件不存在
-----Throwable常用方法測試------
文件不存在
java.io.FileNotFoundException: 文件不存在
不管程序怎樣,這里都會被執(zhí)行
注意事項(xiàng) :try
、 catch
、 finally
、都不可以單獨(dú)使用
到此這篇關(guān)于Java
異常類型及處理詳情的文章就介紹到這了,更多相關(guān)Java
異常類型及處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java導(dǎo)入導(dǎo)出csv格式文件完整版詳解(附代碼)
在Java中你可以使用不同的庫來導(dǎo)出CSV格式的文件,這篇文章主要給大家介紹了關(guān)于Java導(dǎo)入導(dǎo)出csv格式文件的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07IDEA2020如何打開Run Dashboard的方法步驟
這篇文章主要介紹了IDEA2020如何打開Run Dashboard的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java中Runnable和Callable分別什么時(shí)候使用
提到 Java 就不得不說多線程了,就算你不想說,面試官也得讓你說呀,那說到線程,就不得不說Runnable和Callable這兩個家伙了,二者在什么時(shí)候使用呢,下面就來和簡單講講2023-08-08java使用compareTo實(shí)現(xiàn)一個類的對象之間比較大小操作
這篇文章主要介紹了java使用compareTo實(shí)現(xiàn)一個類的對象之間比較大小操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09SpringBoot中啟動時(shí)如何忽略某項(xiàng)檢測
這篇文章主要介紹了SpringBoot中啟動時(shí)如何忽略某項(xiàng)檢測,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11