1.算数异常 java.lang.ArithmeticException
public class RuntimeException {
public static void main(String[] args) {
//发生算数异常
int a = 10;
int b = 0;
int res=a/b;
System.out.println(res);
System.out.println("程序执行完毕");
}
}
运行结果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.monkey1029.RuntimeException.main(RuntimeException.java:10)
解决方法:
public class RuntimeException {
public static void main(String[] args) {
//发生算数异常
int a = 10;
int b = 0;
if(b != 0) {
int res=a/b;
System.out.println(res);
}
System.out.println("程序执行完毕");
}
}
结果:
程序执行完毕
2.数组下标越界异常 java.lang.ArrayIndexOutOfBoundsException:
public class RuntimeException {
public static void main(String[] args) {
// 定义一个长度为5的int类型的数组
int[] arr = new int[5];
int pos = 5;
System.out.println(arr[5]); // 打印出数组中下标为5的元素
}
}
结果:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at com.monkey1029.RuntimeException.main(RuntimeException.java:10)
解决方法:
public class RuntimeException {
public static void main(String[] args) {
// 定义一个长度为5的int类型的数组
int[] arr = new int[5];
int pos = 5;
if(pos<=0 && pos<arr.length) {
System.out.println(arr[5]); // 打印出数组中下标为5的元素
}else {
System.out.println("程序执行完毕");
}
}
}
结果:
程序执行完毕
3.空指针异常 java.lang.NullPointerException
public class RuntimeException {
public static void main(String[] args) {
String str = null;
System.out.println(str.length()); // 调用String类的方法
}
}
结果:
Exception in thread "main" java.lang.NullPointerException
at com.monkey1029.RuntimeException.main(RuntimeException.java:7)
解决方法:
public class RuntimeException {
public static void main(String[] args) {
String str = null;
if(str != null) {
System.out.println(str.length()); // 调用String类的方法
}else {
System.out.println("程序执行完毕");
}
}
}
结果:
程序执行完毕
4.类型转换异常 java.lang.ClassCastException
public class RuntimeExceptionTest {
public static void main(String[] args) {
Exception ex = new Exception();
RuntimeException rex = (RuntimeException)ex;
System.out.println("程序结束了");
}
}
结果:
Exception in thread "main" java.lang.ClassCastException: java.lang.Exception cannot be cast to java.lang.RuntimeException
at com.monkey1029.RuntimeExceptionTest.main(RuntimeExceptionTest.java:7)
解决方法:
public class RuntimeExceptionTest {
public static void main(String[] args) {
Exception ex = new Exception();
if(ex instanceof RuntimeException) { // ex指向的对象是否真正指向 RuntimeException
RuntimeException rex = (RuntimeException)ex;
}
System.out.println("程序结束了");
}
}
结果:
程序结束了
5. 数字格式异常 java.lang.NumberFormatException
public class RuntimeExceptionTest {
public static void main(String[] args) {
String str = "123abc";
System.out.println(Integer.parseInt(str));
System.out.println("程序结束了");
}
}
结果:
Exception in thread "main" java.lang.NumberFormatException: For input string: "123abc"
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 com.monkey1029.RuntimeExceptionTest.main(RuntimeExceptionTest.java:8)
解决方式:
public class RuntimeExceptionTest {
public static void main(String[] args) {
String str = "123abc";
// 判断字符串中的所有字符是否都为数字字符
if(str.matches("\d+")) {
System.out.println(Integer.parseInt(str));
}
System.out.println("程序结束了");
}
}
结果:
程序结束了