javaee 中遇到的異常(Exception)
- 如果輸入了類型不匹配的數據,則會報InputMismathException(輸入不匹配異常)
- 如果訪問超過數組范圍的下標將會報數組下標越界異常:ArrayIndexOutOfBoundsException ( 數組越界異常)
在多態的向下轉型中,如果直接把父類類型轉換為子類類型將會出現類轉換異常ClassCastException(類轉換異常)
- 調用String方法打印字符串時,如果打印的超過字符串長度,將會出現字符串下標越界異常:java.lang.StringIndexOutOfBoundsException(字符串下標越界異常)
public static void main(String[] args) {String str = "abcdefdafa";System.out.println(str.charAt(91));//截圖下標為3的char類型字符返回}
}
- 字符串轉化為基本數據類型時,如果輸入不合適的字符串將會出現數字格式異常:NumberFormatException(數字格式異常)
/*** 字符串 ------> 基本數據類型 XXX.parseXXX();* 注意沒有char(單個字符)類型* @author **/
public class Test2 {public static void main(String[] args) {String str1 = "123.1";//這里應該是整數類型byte byteStr1 = Byte.parseByte(str1);System.out.println(byteStr1);System.out.println("===============");int intStr1 = Integer.parseInt("32");System.out.println(intStr1);}
}
- 在除法中,如果輸入的除數為0,將會出現算數運算異常:ArithmeticException(算數異常)
public class Test1 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("請輸入被除數:");int num1 = input.nextInt();System.out.println("請輸入除數:");int num2 = input.nextInt();System.out.println(num1 + "/" + num2 + "=" + num1 / num2);System.out.println("程序結束");}}
持續更新~~~