版權聲明:本文為部落客原創文章,遵循CC 4.0 by-sa版權協定,轉載請附上原文出處連結和本聲明。
本文連結:https://blog.csdn.net/qq_36050720/article/details/98628990
Java異常處理的五個關鍵字(try、catch、finally、throw、throws):
抛出異常 throw;
聲明異常 throws;
捕獲異常try...catch(Throwable類中定義了3個異常處理的方法);
finally代碼塊;
1. 抛出異常 throw:
執行個體:
定義一個類:
[code]package YiChang;
public class Demo01Throw {
public static void main(String[] args) {
//int[] arr = null
int[] arr = new int[3];
int e = getElement(arr, 3);
System.out.println(e);
}
private static int getElement(int[] arr,int index) {
if(arr == null){
throw new NullPointerException("傳遞的數組的值是null");
}
if(index < 0 || index > arr.length-1){
throw new ArrayIndexOutOfBoundsException("傳遞的索引超出了數組的使用範圍");
}
int ele = arr[index];
return ele;
}
}
運作結果:
[code]Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 傳遞的索引超出了數組的使用範圍
at YiChang.Demo01Throw.getElement(Demo01Throw.java:51)
at YiChang.Demo01Throw.main(Demo01Throw.java:20)
2. 聲明異常 throws:
執行個體:
定義一個類:
[code]package YiChang;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo02Throws {
//public static void main(String[] args) throws FileNotFoundException,IOException {
//public static void main(String[] args) throws IOException {
public static void main(String[] args) throws Exception {
//readFile("c:\\a.txt");
//readFile("d:\\a.txt");
readFile("c:\\a.tx");
}
public static void readFile(String fileName) throws FileNotFoundException,IOException {
if(!fileName.equals("c:\\a.txt")){
throw new FileNotFoundException("傳遞的檔案路徑不是c:\\a.txt");
}
if(fileName.endsWith(".txt")){
System.out.println("檔案名的字尾名不對");
}
System.out.println("路徑沒有問題,讀取檔案");
}
}
運作結果:
[code]Exception in thread "main" java.io.FileNotFoundException: 傳遞的檔案路徑不是c:\a.txt
at YiChang.Demo02Throws.readFile(Demo02Throws.java:48)
at YiChang.Demo02Throws.main(Demo02Throws.java:36)
3.捕獲異常try...catch:
執行個體:
定義一個類:
[code]package YiChang;
import java.io.IOException;
public class Demo03TryCatch {
public static void main(String[] args) throws Exception{
try{
//可能産生異常的代碼
readFile("d:\\a.tx");
}catch(IOException e){ // try中抛出什麼異常對象,catch就定義什麼異常變量,用來接收這個異常對象
//異常的處理邏輯,産生異常對象之後,怎麼處理異常對象
System.out.println("catch - 傳遞的檔案字尾不是.txt");
//System.out.println(e.getMessage()); //檔案的字尾名不對
//System.out.println(e.toString()); //java.io.IOException: 檔案的字尾名不對
//e.printStackTrace();
}
System.out.println("後續代碼");
}
public static void readFile(String fileName) throws IOException {
if(!fileName.endsWith(".txt")){
throw new IOException("檔案的字尾名不對");
}
System.out.println("路徑沒有問題,讀取檔案");
}
}
運作結果:
[code]catch - 傳遞的檔案字尾不是.txt
後續代碼
4.finally代碼塊:
執行個體:
定義一個類:
[code]package YiChang;
import java.io.IOException;
public class Demo04Finally {
public static void main(String[] args) {
try {
//可能會産生異常的代碼
readFile("c:\\a.tx");
} catch (IOException e) {
//異常的處理邏輯
e.printStackTrace();
}finally {
//無論是否出現異常,都會執行
System.out.println("資源釋放");
}
}
public static void readFile(String fileName) throws IOException {
if(!fileName.endsWith(".txt")){
throw new IOException("檔案的字尾名不對");
}
System.out.println("路徑沒有問題,讀取檔案");
}
}
運作結果:
[code]資源釋放
java.io.IOException: 檔案的字尾名不對
at YiChang.Demo04Finally.readFile(Demo04Finally.java:45)
at YiChang.Demo04Finally.main(Demo04Finally.java:29)