異常.png
JVM 的默認(rèn)處理方案
如果程序出現(xiàn)了問(wèn)題,我們沒有做任何處理,最終 JVM 會(huì)做默認(rèn)的處理
- 把異常的名稱,異常的原因及異常出現(xiàn)的位置等信息輸出在控制臺(tái)
- 程序停止執(zhí)行
異常處理
如果程序出現(xiàn)了問(wèn)題,我們需要自己來(lái)處理,有兩種方案:
try ... catch ...
throws
異常處理之 try...catch...
- 格式
try {
可能出現(xiàn)的異常代碼;
} catch(異常類名 變量名) {
異常的處理代碼;
}
- 執(zhí)行流程:
程序從 try 里面的代碼開始執(zhí)行
出現(xiàn)異常,會(huì)自動(dòng)生成一個(gè)異常類對(duì)象,該異常類對(duì)象將被提交給 java 運(yùn)行時(shí)系統(tǒng)
當(dāng) java 運(yùn)行時(shí)系統(tǒng)接收到異常對(duì)象時(shí),會(huì)到 catch 中去找匹配的異常類,找到后進(jìn)行異常的處理
執(zhí)行完畢后,程序還可以繼續(xù)往下執(zhí)行
public static void main(String[] args){
System.out.println("開始");
method();
System.out.println("結(jié)束");
}
public static void method() {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException e){ // new ArrayIndexOutOfBoundsException();
// System.out.println("你訪問(wèn)的數(shù)組索引不存在");
e.printStackTrace();
}
}
Throwable 的成員方法
Throwable的成員方法.png
public static void main(String[] args){
System.out.println("開始");
method();
System.out.println("結(jié)束");
}
public static void method() {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException e){ // new ArrayIndexOutOfBoundsException();
// System.out.println("你訪問(wèn)的數(shù)組索引不存在");
// e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(e.toString());
e.printStackTrace(); // 最全
}
}
編譯時(shí)異常和運(yùn)行時(shí)異常的區(qū)別
java 中的異常被分為兩大類: 編譯時(shí)異常和運(yùn)行時(shí)異常,也被成為受檢異常和非受檢異常
所有的 RuntimeException 類及其子類被稱為運(yùn)行時(shí)異常,其他異常都是編譯時(shí)異常
- 編譯時(shí)異常: 必須顯示處理,否則程序就會(huì)發(fā)生錯(cuò)誤,無(wú)法通過(guò)編譯
- 運(yùn)行時(shí)異常: 無(wú)需顯示處理,也可以和編譯時(shí)異常一樣處理
public static void main(String[] args){
// method();
method2();
}
// 編譯時(shí)異常
public static void method2() {
try{
String s = "2020-02-09";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(s);
System.out.println(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
// 運(yùn)行時(shí)異常
public static void method() {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]); // ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
異常處理之 throws
雖然我們通過(guò) try...catch... 可以對(duì)異常進(jìn)行處理,但是并不是所有的情況我們都有權(quán)限進(jìn)行異常的處理
也就是說(shuō),有些時(shí)候可能出現(xiàn)的異常是我們處理不了的,這個(gè)時(shí)候該怎么辦?
針對(duì)這種情況,java 提供了 throws 的處理方案
- 格式
throws 異常類名;
注意: 這個(gè)格式是跟在方法的括號(hào)后面的
- 編譯時(shí)異常必須要處理,兩種處理方案:
try...catch...
或者throws
,如果采用throws
這種方案,將來(lái)誰(shuí)調(diào)用誰(shuí)處理 - 運(yùn)行時(shí)異常可以不處理,出現(xiàn)問(wèn)題后,需要我們回來(lái)修改代碼
public static void main(String[] args){
System.out.println("開始");
// method();
try {
method2();
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("結(jié)束");
}
// 編譯時(shí)異常
public static void method2() throws ParseException {
String s = "2020-02-09";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(s);
System.out.println(d);
}
// 運(yùn)行時(shí)異常
public static void method() throws ArrayIndexOutOfBoundsException{
int[] arr = {1, 2, 3};
System.out.println(arr[3]); // ArrayIndexOutOfBoundsException
}
自定義異常
- 格式:
public class 異常類名 extends Exception {
無(wú)參構(gòu)造
帶參構(gòu)造
}
- 范例:
public class ScoreException extends Exception {
public ScoreException() {}
public ScoreException(String message) {
super(message);
}
}
public class ScoreException extends Exception {
public ScoreException() {}
public ScoreException(String message) {
super(message);
}
}
public class Teacher {
public void checkScore(int score) throws ScoreException {
if (score<0 || score>100) {
throw new ScoreException("你給的分?jǐn)?shù)有誤,應(yīng)該在0-100之間");
} else {
System.out.println("分?jǐn)?shù)正常");
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入分?jǐn)?shù): ");
int score = sc.nextInt();
Teacher t = new Teacher();
try {
t.checkScore(score);
} catch (ScoreException e) {
e.printStackTrace();
}
}
throws 和 throw 的區(qū)別
- throws
- 用在方法聲明后面,跟的是異常類名
- 表示拋出異常,由該方法的調(diào)用者來(lái)處理
- 表示出現(xiàn)異常的一種可能性,并不一定會(huì)發(fā)生這些異常
- throw
- 用在方法體內(nèi),跟的是異常對(duì)象名
- 表示拋出異常,由方法體內(nèi)的語(yǔ)句處理
- 執(zhí)行throw 一定拋出了某種異常