1.異常的分類(lèi)
1.Throwable(異常根類(lèi))
Error產(chǎn)生的異常:虛擬機(jī)錯(cuò)誤、內(nèi)存溢出、線程死鎖
Excepetion:程序本身可以處理的異常。異常處理通常針對(duì)這種類(lèi)型異常的處理非檢查異常 Unchecked Exception:空指針異常、數(shù)組下標(biāo)越界異常、算數(shù)異常、類(lèi)型轉(zhuǎn)換異常; (是可以不檢查的)
檢查異常 Checked Exception:(系統(tǒng)要求必須檢查)

2.異常處理
異常處理
3.try...catch...final
package com.imooc.test;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TryDemoOne {
public static void main(String[] args) {
/* // 要求:定義兩個(gè)整數(shù),輸出兩數(shù)之商
int one=12;
int two=2;
System.out.println("one和two的商是:"+ (one/two));
*/
// 要求:定義兩個(gè)整數(shù),接受用戶(hù)的鍵盤(pán)輸入,輸出兩數(shù)之商
Scanner input=new Scanner(System.in);
System.out.println("=====運(yùn)算開(kāi)始=====");
try{
System.out.print("請(qǐng)輸入第一個(gè)整數(shù):");
int one=input.nextInt();
System.out.print("請(qǐng)輸入第二個(gè)整數(shù):");
int two=input.nextInt();
System.out.println("one和two的商是:"+ (one/two));
}catch(ArithmeticException e){
System.exit(1);//終止程序運(yùn)行 可以通過(guò)Java API 查看具體的用法https://docs.oracle.com/javase/8/docs/api/ Java.lang包下的system類(lèi)的方法
System.out.println("除數(shù)不允許為零");
e.printStackTrace();
}catch(InputMismatchException e){
System.out.println("請(qǐng)輸入整數(shù)");
e.printStackTrace();
}catch(Exception e){//一般用在最后一個(gè)catch,在前面的類(lèi)型沒(méi)有檢查出來(lái),用這個(gè)進(jìn)行包含
System.out.println("出錯(cuò)啦~~");
e.printStackTrace();
}finally{//無(wú)論是否發(fā)生異常,總會(huì)執(zhí)行這個(gè)
System.out.println("=====運(yùn)算結(jié)束=====");
}
}
}
4.catch 中的 return
package com.imooc.test;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TryDemoTwo {
public static void main(String[] args) {
// TODO Auto-generated method stub
int result=test();
System.out.println("one和two的商是:"+ result);
}
public static int test(){
Scanner input=new Scanner(System.in);
System.out.println("=====運(yùn)算開(kāi)始=====");
try{
System.out.print("請(qǐng)輸入第一個(gè)整數(shù):");
int one=input.nextInt();
System.out.print("請(qǐng)輸入第二個(gè)整數(shù):");
int two=input.nextInt();
return one/two;
}catch(ArithmeticException e){
System.out.println("除數(shù)不允許為零");
return 0;
}finally{
System.out.println("=====運(yùn)算結(jié)束=====");
// return -100000;//無(wú)論上面進(jìn)入哪個(gè)catch,內(nèi)部的return都會(huì)失效,最后由finally中的return返回
}
}
}
5throws
通過(guò)throws聲明將要拋出何種類(lèi)型的異常,通過(guò)throw將產(chǎn)生的異常拋出1.public static int test() throws Exception 在函數(shù)后面聲明可能產(chǎn)生的異常。
2.throws后面接多個(gè)異常類(lèi)型,中間用逗號(hào)分隔
- RuntimeException都是UncheckedException非檢查型異常。當(dāng)throws非檢查型異常時(shí),在調(diào)用方法處,編譯器不會(huì)要求對(duì)異常進(jìn)行處理。這里的ArithmeticException 和 InputMismatchException就是非檢查型異常,就算你不去try,編譯器也不會(huì)報(bào)錯(cuò)。如果需要提醒檢查,可以通過(guò)加文檔注釋的方法,提醒
4.Exception類(lèi)型屬于檢查型異常,編譯器會(huì)進(jìn)行錯(cuò)誤提示必須處理,即必須try...catch
可能直接使用一個(gè)Exception,然后在后面再對(duì)拋出的類(lèi)進(jìn)行細(xì)分
public static int test() throws Exception {
Scanner input = new Scanner(System.in);
int one = input.nextInt();
int two = input.nextInt();
return one/two;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println(test());
} catch (ArithmeticException e) {
System.out.println("被除數(shù)不能為0");
e.printStackTrace();
} catch(InputMismatchException e) {
System.out.println("數(shù)字格式錯(cuò)誤,請(qǐng)輸入一個(gè)整數(shù)");
e.printStackTrace();
} catch(Exception e) {
}
}
也可以直接throws的后面就接上可能拋出的具體異常子類(lèi)
public static int test() throws ArithmeticException,InputMismatchException {
Scanner input = new Scanner(System.in);
int one = input.nextInt();
int two = input.nextInt();
return one/two;
}
/**
* 測(cè)試接收數(shù)據(jù)相除結(jié)果的方法
* @return 兩個(gè)接收數(shù)據(jù)的商
* @throws ArithmeticException
* @throws InputMismatchException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println(test());
} catch (ArithmeticException e) {
System.out.println("被除數(shù)不能為0");
e.printStackTrace();
} catch(InputMismatchException e) {
System.out.println("數(shù)字格式錯(cuò)誤,請(qǐng)輸入一個(gè)整數(shù)");
e.printStackTrace();
}
}
6 通過(guò)throw 主動(dòng)拋出異常,進(jìn)行一些業(yè)務(wù)邏輯實(shí)現(xiàn)
* throw 拋出異常對(duì)象的處理方案
* 1.通過(guò)try...catch 包含throw語(yǔ)句--自己拋出自己處理,
* 2.通過(guò)throws在方法聲明出拋出異常類(lèi)型--誰(shuí)調(diào)用誰(shuí)處理--調(diào)用者可以自己處
理,也可以繼續(xù)往上拋,throws后面接的異常類(lèi)與throw對(duì)象相同的類(lèi)型或者其父類(lèi)。
throws Exception 可以換成 throws Throwable,但不可以換成子類(lèi)
package com.imooc.test;
import java.util.Scanner;
public class TryDemoFour {
public static void main(String[] args) {
// TODO Auto-generated method stub
//testAge1();
try {
testAge2();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* throw 拋出異常對(duì)象的處理方案
* 1.通過(guò)try...catch 包含throw語(yǔ)句--自己拋出自己處理
* 2.通過(guò)throws在方法聲明出拋出異常類(lèi)型--誰(shuí)調(diào)用誰(shuí)處理--調(diào)用者可以自己處理,也可以繼續(xù)往上拋
* @throws Exception
*/
//描述酒店的入住規(guī)則:限定年齡,18歲以下,80歲以上的住客必須由親友陪同
public static void testAge1() {
try {
System.out.println("請(qǐng)輸入年齡:");
Scanner input = new Scanner(System.in);
int age = input.nextInt();
if(age<18 || age>80) {
throw new Exception("18歲以下,80歲以上的住客必須由親友陪同");//Exception有有一個(gè)構(gòu)造函數(shù),可以傳入字符串
} else {
System.out.println("歡迎光臨");
}
}catch(Exception e) {
e.printStackTrace();
}
}
public static void testAge2() throws Exception {
System.out.println("請(qǐng)輸入年齡:");
Scanner input = new Scanner(System.in);
int age = input.nextInt();
if(age<18 || age>80) {
throw new Exception("18歲以下,80歲以上的住客必須由親友陪同");//Exception有有一個(gè)構(gòu)造函數(shù),可以傳入字符串
} else {
System.out.println("歡迎光臨");
}
}
}
7.自定義異常
自定義一個(gè)類(lèi)繼承Exception 即可,這個(gè)類(lèi)的用法與其他的完全相同
package com.imooc.test;
public class HotelException extends Exception {
public HotelException() {
super("18歲以下,80歲以上的住客必須由親友陪同");//使用Exception 進(jìn)行構(gòu)造函數(shù)參數(shù)的傳遞
}
@Override
public String getMessage() {//這個(gè)函數(shù)可以獲取傳入構(gòu)造函數(shù)的參數(shù)
// TODO Auto-generated method stub
return super.getMessage();
}
}
8 異常鏈
1.只有最后一個(gè)異常拋出來(lái)了。
package com.imooc.test;
public class TryDemoFive {
public static void main(String[] args) {
try {
test3();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test1() throws Exception {
throw new Exception("test1異常");
}
public static void test2() throws Exception {
try {
test1();
} catch (Exception e) {
throw new Exception("test2異常");
}
}
public static void test3() throws Exception {
try {
test2();
} catch (Exception e) {
throw new Exception("test3異常");
}
}
}
輸出結(jié)果:
java.lang.Exception: test3異常
at com.imooc.test.TryDemoFive.test3(TryDemoFive.java:29)
at com.imooc.test.TryDemoFive.main(TryDemoFive.java:7)
2.打印出異常鏈的全部信息
https://docs.oracle.com/javase/8/docs/api/
方案一:
Throwable(String message, Throwable cause)
方案2:
initCause(Throwable cause)
package com.imooc.test;
public class TryDemoFive {
public static void main(String[] args) {
try {
test3();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test1() throws Exception {
throw new Exception("test1異常");
}
public static void test2() throws Exception {
try {
test1();
} catch (Exception e) {
throw new Exception("test2異常",e);//方案1:將上面捕獲的異常對(duì)象傳入
}
}
public static void test3() throws Exception {
try {
test2();
} catch (Exception e) {
//throw new Exception("test3異常",e);
Exception e1 = new Exception("test3異常");//方案2:initCause方法
e1.initCause(e);
throw e1;
}
}
}
