淺析Java中的異常處理機(jī)制
異常處理機(jī)制
1、拋出異常
2、捕獲異常
3、異常處理五個(gè)關(guān)鍵字:
try、catch、finally、throw、throws
注意:假設(shè)要捕獲多個(gè)異常:需要按照層級(jí)關(guān)系(異常體系結(jié)構(gòu)) 從小到大!
package exception;
/**
* Java 捕獲和拋出異常:
* 異常處理機(jī)制
* 1、拋出異常
* 2、捕獲異常
* 3、異常處理五個(gè)關(guān)鍵字
* try、catch、finally、throw、throws
* 注意:假設(shè)要捕獲多個(gè)異常:需要按照層級(jí)關(guān)系(異常體系結(jié)構(gòu)) 從小到大!
*/
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
/**
* try catch 是一個(gè)完整的機(jī)構(gòu)體,finally 可以不要
* 假設(shè)IO流,或者跟資源相關(guān)的東西,最后需要關(guān)閉,關(guān)閉的操作就放在 finally 中
*/
try { //try 監(jiān)控區(qū)域
System.out.println(a / b);
} catch (ArithmeticException exception){ //catch(想要捕獲的異常類(lèi)型) 捕獲異常
System.out.println("程序出現(xiàn)異常,變量b不能為0");
} finally { //處理善后工作
System.out.println("finally");
}
System.out.println("-------------- 分隔符 --------------");
try {
new Test().a(); //無(wú)限循環(huán)
} catch (Error error){
System.out.println("Error");
} catch (Exception exception){
System.out.println("Exception");
} catch (Throwable throwable){
System.out.println("Throwable");
} finally {
System.out.println("finally");
}
}
public void a(){
b();
}
public void b() {
a();
}
}
捕獲異常
快捷鍵:選中代碼 Ctrl + Alt + T
捕獲異常的好處:程序不會(huì)意外的停止,try catch 捕獲異常后程序會(huì)正常的往下執(zhí)行
package exception;
/**
* 捕獲異??旖萱I
* 選中代碼后:Ctrl + Alt + T
* 如:
* 選中 System.out.println(a / b);
* 然后快捷鍵 Ctrl + Alt + T
*/
public class Test2 {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {
System.out.println(a / b);
} catch (Exception exception) {
exception.printStackTrace(); //打印錯(cuò)誤的棧信息
} finally {
}
}
}
拋出異常
1、在方法中拋出異常:throw
2、在方法上拋出異常:throws
package exception;
/**
* 捕獲異常
* 拋出異常
*/
public class Test3 {
public static void main(String[] args) {
/**
* 方法中拋出異常
*/
new Test3().test(1,0); //匿名內(nèi)部類(lèi)直接調(diào)用
System.out.println("------------ 分隔符 -------------");
/**
* 方法上拋出異常
* 捕獲異常的好處:
* 程序不會(huì)意外的停止,try catch 捕獲異常后程序會(huì)正常的往下執(zhí)行
*/
try {
new Test3().test2(1,0); //匿名內(nèi)部類(lèi)直接調(diào)用
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
/**
* 在方法中拋出異常:throw
* @param a
* @param b
*/
public void test(int a, int b){
if (b == 0){ //throw
throw new ArithmeticException(); //主動(dòng)拋出異常,一般在方法中使用
}
System.out.println(a / b);
}
/**
* 假設(shè)在方法中處理不了這個(gè)異常,就在方法上拋出異常,然后捕獲異常
* 在方法上拋出異常:throws
* @param a
* @param b
* @throws ArithmeticException
*/
public void test2(int a, int b) throws ArithmeticException{
if (b == 0){
throw new ArithmeticException();
}
}
}
以上就是淺析Java中的異常處理機(jī)制的詳細(xì)內(nèi)容,更多關(guān)于Java 異常處理機(jī)制的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java實(shí)現(xiàn)統(tǒng)計(jì)在線人數(shù)功能的方法詳解
很多人在筆試或者面試中問(wèn)到:現(xiàn)在要你實(shí)現(xiàn)一個(gè)統(tǒng)計(jì)在線人數(shù)的功能,你該怎么設(shè)計(jì)?不知道的朋友,這篇文章就來(lái)告訴你具體實(shí)現(xiàn)方法2022-08-08
SpringBoot中使用AOP切面編程實(shí)現(xiàn)登錄攔截功能
本文介紹了如何使用AOP切面編程實(shí)現(xiàn)Spring Boot中的登錄攔截,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-12-12
Spring Boot項(xiàng)目如何同時(shí)支持HTTP和HTTPS協(xié)議的實(shí)現(xiàn)
這篇文章主要介紹了Spring Boot項(xiàng)目如何同時(shí)支持HTTP和HTTPS協(xié)議的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Java反射中java.beans包學(xué)習(xí)總結(jié)
本篇文章通過(guò)學(xué)習(xí)Java反射中java.beans包,吧知識(shí)點(diǎn)做了總結(jié),并把相關(guān)內(nèi)容做了關(guān)聯(lián),對(duì)此有需要的朋友可以學(xué)習(xí)參考下。2018-02-02

