Java異常處理的五個(gè)關(guān)鍵字
異常:異常有的是因?yàn)橛脩翦e(cuò)誤引起,有的是程序錯(cuò)誤引起的,還有其它一些是因?yàn)槲锢礤e(cuò)誤引起的。
異常處理關(guān)鍵字:try、catch、finally、throw、throws
注意事項(xiàng):
- 錯(cuò)誤不是異常,而是脫離程序員控制的問(wèn)題。
- 所有的異常類是從 java.lang.Exception 類繼承的子類。
- 異常類有兩個(gè)主要的子類:IOException 類和 RuntimeException 類。
- Java有很多的內(nèi)置異常類。
異常大致分類:
- 用戶輸入了非法數(shù)據(jù)。
- 要打開(kāi)的文件不存在。
- 網(wǎng)絡(luò)通信時(shí)連接中斷,或者JVM內(nèi)存溢出。
語(yǔ)法:
try{ //需要監(jiān)聽(tīng)的代碼塊 }
catch(異常類型 異常名稱/e){ //對(duì)捕獲到try監(jiān)聽(tīng)到的出錯(cuò)的代碼塊進(jìn)行處理 throw 異常名稱/e; //thorw表示拋出異常 throw new 異常類型(“自定義”); }
finally{ //finally塊里的語(yǔ)句不管異常是否出現(xiàn),都會(huì)被執(zhí)行 } 修飾符 返回值 方法名 () throws 異常類型{ //throws只是用來(lái)聲明異常,是否拋出由方法調(diào)用者決定 //代碼塊 }
代碼例子:(try與catch與finally)
public class ExceptionTest { public static void main(String[] args) { Scanner input=new Scanner(System.in); try{ //監(jiān)聽(tīng)代碼塊 int a=input.nextInt(); int b=input.nextInt(); double sum=a/b; System.out.println(sum); } catch(InputMismatchException e){ System.out.println("只能輸入數(shù)字"); } catch(ArithmeticException e){ System.out.println("分母不能為0"); } catch(Exception e){ //Exception是所有異常的父類 System.out.println("發(fā)生了其他異常"); } finally{ //不管是否出現(xiàn)異常,finally一定會(huì)被執(zhí)行 System.out.println("程序結(jié)束"); } } }
代碼例子:(throw關(guān)鍵字)
import java.util.InputMismatchException; import java.util.Scanner; public class ExceptionTest { public static void main(String[] args) { Scanner input=new Scanner(System.in); try{ //監(jiān)聽(tīng)代碼塊 int a=input.nextInt(); int b=input.nextInt(); double sum=a/b; System.out.println(sum); } catch(InputMismatchException e){ //catch(異常類型 異常名稱) System.out.println("只能輸入數(shù)字"); throw e; //拋出catch捕捉到的異常 //throw new InputMismatchException(); 同上 } catch(ArithmeticException e){ System.out.println("分母不能為0"); throw new ArithmeticException("分母為0拋出異常"); //拋出ArithmeticException異常 } catch(Exception e){ //Exception是所有異常的父類 System.out.println("發(fā)生了其他異常"); } finally{ //不管是否出現(xiàn)異常,finally一定會(huì)被執(zhí)行 System.out.println("程序結(jié)束"); } } }
代碼例子:(throws)
public class Throws { int a=1; int b=0; public void out() throws ArithmeticException{ //聲明可能要拋出的異常,可以有多個(gè)異常,逗號(hào)隔開(kāi) try{ //監(jiān)聽(tīng)代碼塊 int sum=a/b; System.out.println(sum); } catch(ArithmeticException e){ System.out.println("分母不能為0"); } finally{ //不管是否出現(xiàn)異常,finally一定會(huì)被執(zhí)行 System.out.println("程序結(jié)束"); } } public static void main(String[] args){ Throws t=new Throws(); t.out(); //調(diào)用方法 throw new ArithmeticException("分母為0拋出異常"); //由調(diào)用的方法決定是否要拋出異常 /* * 第二種拋出方式 */ // ArithmeticException a=new ArithmeticException("分母為0拋出異常"); // throw a; } }
相關(guān)文章
SpringBoot整合HikariCP數(shù)據(jù)庫(kù)連接池方式
這篇文章主要介紹了SpringBoot整合HikariCP數(shù)據(jù)庫(kù)連接池方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03SpringBoot 添加JSP 支持并附帶在IDEA下創(chuàng)建JSP文件【測(cè)試無(wú)誤】
這篇文章主要介紹了SpringBoot 添加JSP 支持并附帶在IDEA下創(chuàng)建JSP文件的相關(guān)知識(shí),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05postgresql 實(shí)現(xiàn)16進(jìn)制字符串轉(zhuǎn)10進(jìn)制數(shù)字
這篇文章主要介紹了postgresql 實(shí)現(xiàn)16進(jìn)制字符串轉(zhuǎn)10進(jìn)制數(shù)字操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02在Spring Boot中淺嘗內(nèi)存泄漏的實(shí)戰(zhàn)記錄
本文給大家分享在Spring Boot中淺嘗內(nèi)存泄漏的實(shí)戰(zhàn)記錄,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-04-04SpringBoot中靜態(tài)訪問(wèn)配置屬性的解決方案對(duì)比
在SpringBoot開(kāi)發(fā)中,靜態(tài)訪問(wèn)配置信息是一個(gè)常見(jiàn)需求,尤其是在工具類中直接獲取配置值,下面我們就來(lái)看看幾個(gè)常用的方法,大家可以根據(jù)需要選擇2025-03-03