溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JAVA拋出異常的形式有哪些

發(fā)布時(shí)間:2021-11-19 16:39:39 來源:億速云 閱讀:160 作者:iii 欄目:編程語言

這篇文章主要介紹“JAVA拋出異常的形式有哪些”,在日常操作中,相信很多人在JAVA拋出異常的形式有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”JAVA拋出異常的形式有哪些”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

一、系統(tǒng)自動(dòng)拋出異常

當(dāng)程序語句出現(xiàn)一些邏輯錯(cuò)誤、主義錯(cuò)誤或者類型轉(zhuǎn)換錯(cuò)誤時(shí),系統(tǒng)會(huì)自動(dòng)拋出異常

例一

public static void main(String[] args) {int a = 5;int b = 0;System.out.println( a / b);}

運(yùn)行結(jié)果,系統(tǒng)會(huì)自動(dòng)拋出ArithmeticException異常

Exception in thread "main" java.lang.ArithmeticException: / by zeroat io.renren.modules.sys.controller.SysUserController.main(SysUserController.java:154)

例二

public static void main(String[] args) {String str = "abc";System.out.println(Integer.parseInt(str));}

運(yùn)行結(jié)果,系統(tǒng)會(huì)拋出NumberFormatException異常

Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.lang.Integer.parseInt(Integer.java:580)at java.lang.Integer.parseInt(Integer.java:615)at io.renren.modules.sys.controller.SysUserController.main(SysUserController.java:153)

二、throw

throw是語句拋出一個(gè)異常,一般是在代碼的內(nèi)部,當(dāng)程序出現(xiàn)某種邏輯錯(cuò)誤時(shí)同程序主動(dòng)拋出某種特定類型的異常

public static void main(String[] args) {String str = "NBA";if (str.equals("NBA")) {throw new NumberFormatException();} else {System.out.println(str);}}

運(yùn)行結(jié)果,系統(tǒng)會(huì)拋出NumberFormatException異常

Exception in thread "main" java.lang.NumberFormatExceptionat io.renren.modules.sys.controller.SysUserController.main(SysUserController.java:154)

三、throws

throws是方法可能會(huì)拋出一個(gè)異常(用在聲明方法時(shí),表示該方法可能要拋出異常)

public void function() throws Exception{......}

當(dāng)某個(gè)方法可能會(huì)拋出某種異常時(shí)用于throws 聲明可能拋出的異常,然后交給上層調(diào)用它的方法程序處理

public static void testThrows() throws NumberFormatException {String str = "NBA";System.out.println(Integer.parseInt(str));}public static void main(String[] args) {try {testThrows();} catch (NumberFormatException e) {e.printStackTrace();System.out.println("非數(shù)直類型不能強(qiáng)制類型轉(zhuǎn)換");}}

運(yùn)行結(jié)果

java.lang.NumberFormatException: For input string: "NBA"at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.lang.Integer.parseInt(Integer.java:580)at java.lang.Integer.parseInt(Integer.java:615)at io.renren.modules.sys.controller.SysUserController.testThrows(SysUserController.java:153)at io.renren.modules.sys.controller.SysUserController.main(SysUserController.java:158)非數(shù)直類型不能強(qiáng)制類型轉(zhuǎn)換

throw與throws的比較

1、throws出現(xiàn)在方法函數(shù)頭,而throw出現(xiàn)在函數(shù)體。

2、throws表示出現(xiàn)異常的一種可能性,并不一定會(huì)發(fā)生這些異常,throw則是拋出了異常,執(zhí)行throw則一定拋出了某種異常對(duì)象。

3、兩者都是消極處理異常的方式(這里的消極并不是說這種方式不好),只是拋出或者可能拋出異常,但不會(huì)由函數(shù)去處理異常,真正的處理異常由函數(shù)的上層調(diào)用處理。

編程習(xí)慣

1、在寫程序時(shí),對(duì)可能會(huì)出現(xiàn)異常的部分通常要用try{…}catch{…}去捕捉它并對(duì)它進(jìn)行處理;

2、用try{…}catch{…}捕捉了異常之后一定要對(duì)在catch{…}中對(duì)其進(jìn)行處理,那怕是最簡(jiǎn)單的一句輸出語句,或棧輸入e.printStackTrace();

3、如果是捕捉IO輸入輸出流中的異常,一定要在try{…}catch{…}后加finally{…}把輸入輸出流關(guān)閉;

4、如果在函數(shù)體內(nèi)用throw拋出了某種異常,最好要在函數(shù)名中加throws拋異常聲明,然后交給調(diào)用它的上層函數(shù)進(jìn)行處理。

到此,關(guān)于“JAVA拋出異常的形式有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI