Java異常類型及處理詳情
異常結(jié)構(gòu)為:
Throwable 為頂級(jí)父類
- 子類
Error為嚴(yán)重報(bào)錯(cuò) , - 子類
Exception就是我們所說(shuō)的異常了
一、異常處理的關(guān)鍵字
java中處理異常的有五個(gè)關(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)建一個(gè)數(shù)組
int [] arr = { 2, 4, 56 ,5};
// 根據(jù)索引找到對(duì)應(yīng)的元素
int index = 4;
int element = getElement(arr,index);
System.out.println(element);
System.out.println("owo"); // 運(yùn)行錯(cuò)誤 無(wú)法繼續(xù)
}
/** throw 拋出異常 提醒你必須處理 */
public static int getElement(int [] arr, int index){
// 判斷數(shù)組索引是否越界
if (index < 0 || index > arr.length -1){
/**
* 條件滿足越界 當(dāng)執(zhí)行到throw拋出異常后就無(wú)法運(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 該文件不存在 是一個(gè)錯(cuò)誤 也就是異常 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 捕獲異常
* */
// 可能會(huì)生成的異常
try { // 捕獲或者聲明
read("b.txt");
} catch (FileNotFoundException e) { // 使用某種捕獲,實(shí)現(xiàn)對(duì)異常的處理
System.out.println(e);
/**
* Throwable中的查看方法
* getMessage 獲取異常信息 提示給用戶看的
* toString 獲取異常的類型和異常描述(不用)
* printStackTrace
* */
System.out.println("Throwable常用方法測(cè)試");
System.out.println(e.getMessage()); // 文件不存在
System.out.println(e.toString());
e.printStackTrace();
} finally {
System.out.println("不管程序怎樣,這里都會(huì)被執(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常用方法測(cè)試------
文件不存在
java.io.FileNotFoundException: 文件不存在
不管程序怎樣,這里都會(huì)被執(zhí)行
注意事項(xiàng) :try、 catch、 finally、都不可以單獨(dú)使用
到此這篇關(guān)于Java異常類型及處理詳情的文章就介紹到這了,更多相關(guān)Java異常類型及處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springmvc處理模型數(shù)據(jù)ModelAndView過(guò)程詳解
這篇文章主要介紹了springmvc處理模型數(shù)據(jù)ModelAndView過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Java實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出數(shù)據(jù)庫(kù)的方法示例
這篇文章主要介紹了Java實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式分析了java針對(duì)Excel的讀寫及數(shù)據(jù)庫(kù)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08
Java多線程中的單例模式兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Java多線程中的單例模式兩種實(shí)現(xiàn)方式的相關(guān)資料,需要的朋友可以參考下2017-04-04
SpringBoot處理全局統(tǒng)一異常的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot處理全局統(tǒng)一異常的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

