1.NullPointerException: 空指標例外
範例:
public static void main(String[] args) { String str = null; //此处报空指针异常 System.out.println(str.length()); }
控制台輸出的例外訊息為:
Exception in thread "main" java.lang.NullPointerException at cn.com.gjw.MyClass.main(MyClass.java:7)
2.ClassCastException: 類型強制轉換例外
#範例:
public static void main(String[] args) { // 类型强制转换异常 Object x = new String("String"); System.out.println((Integer) x); }
控制台輸出的例外訊息為:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at cn.com.gjw.MyClass.main(MyClass.java:7)
3.ArrayIndexOutOfBoundsException: 陣列下標越界異常
範例:
public static void main(String[] args) { int arr[] = {1,2}; // 此处报数组下标越界异常 System.out.println(arr[2]); }
控制台輸出的例外資訊為:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at cn.com.gjw.MyClass.main(MyClass.java:7)
#4.ArithmeticException :算術運算例外
範例:
public static void main(String[] args) { // 整数0做了分母,报算术运算异常 System.out.println(1 / 0); }
控制台輸出的例外資訊為:
Exception in thread "main" java.lang.ArithmeticException: / by zero at cn.com.gjw.MyClass.main(MyClass.java:6)
5.NumberFormatException: 數字格式例外
範例:
public static void main(String[] args) { // 将字符串“it”转换为Integer类型的,当然会报数字格式异常啦 System.out.println(Integer.parseInt("it")); }
控制台輸出的例外資訊為:
Exception in thread "main" java.lang.NumberFormatException: For input string: "it" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:492) at java.lang.Integer.parseInt(Integer.java:527) at cn.com.gjw.MyClass.main(MyClass.java:6)
推薦教學:Java教學
#以上是java中常見的幾種異常有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
3 週前ByDDD
Windows 11 KB5054979中的新功能以及如何解決更新問題
2 週前ByDDD
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版
中文版,非常好用

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具