異常try?catch的常見四類方式(案例代碼)
第1類:嵌套模式
package day14;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Demo0301多個(gè)異常異常的嵌套 {
public static void main(String[] args) {
String str=null;
try {
//多個(gè)異常的處理方式一:異常嵌套
try {
//str為null,有可能會(huì)報(bào)空指針異常;
InputStream is=new FileInputStream(str);
} catch (NullPointerException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}第二類:分而治之方式
package day14;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Demo0302多個(gè)異常異常的分別處理 {
public static void main(String[] args) {
String str = null;
try {
//多個(gè)異常的處理方式一:異常嵌套
InputStream is = new FileInputStream(str);
//針對不同的異常,我分來來處理;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}第三類:異常合并方式
package day14;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Demo0303多個(gè)異常異常的合并分開處理 {
public static void main(String[] args) {
String str = null;
try {
//多個(gè)異常的處理方式一:異常嵌套
InputStream is = new FileInputStream(str);
//針對不同的異常,捕獲的時(shí)候,合并到一起,處理的時(shí)候,分開;
} catch (NullPointerException | FileNotFoundException e){
if(e instanceof NullPointerException){
System.out.println("空指針異常");
}else if(e instanceof FileNotFoundException){
System.out.println("文件沒有找到");
}
}
}
}第四類:大合并方式
package day14;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Demo0304多個(gè)異常異常的合并一次處理 {
public static void main(String[] args) {
String str = null;
try {
//多個(gè)異常的處理方式一:異常嵌套
InputStream is = new FileInputStream(str);
//針對不同的異常,捕獲的時(shí)候,合并到一起,處理的時(shí)候,一起處理;
} catch (Exception e){
System.out.println("異常的原因:"+e.getMessage());
}
}
}到此這篇關(guān)于異常try catch的常見四類方式的文章就介紹到這了,更多相關(guān)異常try catch內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用poi自定義excel標(biāo)題頭并導(dǎo)出方式
這篇文章主要介紹了java使用poi自定義excel標(biāo)題頭并導(dǎo)出方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
如何實(shí)現(xiàn)Java的ArrayList經(jīng)典實(shí)體類
ArrayList是Java集合框架中一個(gè)經(jīng)典的實(shí)現(xiàn)類。他比起常用的數(shù)組而言,明顯的優(yōu)點(diǎn)在于,可以隨意的添加和刪除元素而不需考慮數(shù)組的大小。下面跟著小編一起來看下吧2017-02-02
基于Java實(shí)現(xiàn)的Dijkstra算法示例
這篇文章主要介紹了基于Java實(shí)現(xiàn)的Dijkstra算法示例,一個(gè)比較典型的算法示例,需要的朋友可以參考下2014-07-07
SpringBoot基于Redis實(shí)現(xiàn)生成全局唯一ID的方法
在項(xiàng)目中生成全局唯一ID有很多好處,生成全局唯一ID有助于提高系統(tǒng)的可用性、數(shù)據(jù)的完整性和安全性,同時(shí)也方便數(shù)據(jù)的管理和分析,所以本文給大家介紹了SpringBoot基于Redis實(shí)現(xiàn)生成全局唯一ID的方法,文中有詳細(xì)的代碼講解,需要的朋友可以參考下2023-12-12
解決IntelliJ IDEA創(chuàng)建spring boot無法連接http://start.spring.io/問題
這篇文章主要介紹了解決IntelliJ IDEA創(chuàng)建spring boot無法連接http://start.spring.io/問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
淺談SpringBoot 中關(guān)于自定義異常處理的套路
這篇文章主要介紹了淺談SpringBoot 中關(guān)于自定義異常處理的套路,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
spring mvc中的@PathVariable獲得請求url中的動(dòng)態(tài)參數(shù)
本文主要介紹了spring mvc中的@PathVariable獲得請求url中的動(dòng)態(tài)參數(shù)的代碼。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02

