淺談Java異常的Exception e中的egetMessage()和toString()方法的區(qū)別
Exception e中e的getMessage()和toString()方法的區(qū)別:
示例代碼1:
public class TestInfo { private static String str =null; public static void main(String[] args) { System.out.println("test exception"); try { if(str.equals("name")){ System.out.println("test exception"); } } catch (Exception e) { System.out.println(e.toString()); System.out.println(e.getMessage()); } } }
輸出結(jié)果:
; font-family:" microsoft="" background-color:="" /> null
示例代碼2:
public class TestInfo { private static int m = 0; public static void main(String[] args) { System.out.println("test exception"); try { m = 899/0; } catch (Exception e) { System.out.println(e.toString()); System.out.println(e.getMessage()); } } }
輸出結(jié)果:
java.lang.ArithmeticException: / by zero / by zero
總結(jié):由此可以看出,e.toString()獲取的信息包括異常類型和異常詳細(xì)消息,而e.getMessage()只是獲取了異常的詳細(xì)消息字符串。
以上這篇淺談Java異常的Exception e中的egetMessage()和toString()方法的區(qū)別就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot過濾器禁止ip頻繁訪問功能實(shí)現(xiàn)
這篇文章主要介紹了Springboot過濾器禁止ip頻繁訪問功能實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04Spring Cache實(shí)現(xiàn)緩存技術(shù)
在面對公司緩存技術(shù)混亂時,張三基于Spring框架自研緩存解決方案,展現(xiàn)了他的專業(yè)技術(shù)能力和積極工作態(tài)度,他通過問題診斷、技術(shù)選型、編碼測試、文檔編寫和部署監(jiān)控等一系列步驟,確保了新緩存系統(tǒng)的功能正確性和性能穩(wěn)定性2024-10-10Springboot?hibernate-validator?6.x快速校驗(yàn)示例代碼
這篇文章主要介紹了Springboot?hibernate-validator?6.x校驗(yàn),本文以6.2.1.Final版本為例解決了log4j版本的漏洞問題,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12Spring @ComponentScan注解使用案例詳細(xì)講解
@ComponentScan注解的作用可以簡述為:將項(xiàng)目中所有被@Component注解直接或者間接標(biāo)記的類---->組裝成BeanDefinition---->然后以key=beanName, value=BeanDefinition的形式存儲,為后續(xù)生成bean對象做準(zhǔn)備2023-03-03Mybatis-Plus中g(shù)etOne方法獲取最新一條數(shù)據(jù)的示例代碼
這篇文章主要介紹了Mybatis-Plus中g(shù)etOne方法獲取最新一條數(shù)據(jù),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05