java常见的运行时异常

(1)除0发生的算术异常(ArithmeticException)

public class Runtime_01 {
	public static void main(String[] args) {
		for (int i = 10; i > 0; i--) {
			System.out.print(" " + (i / (i - 1)));
			if (i % 5 == 0)
				System.out.println();
		}
	}
}

这里写图片描述

(2)数组下标越界异常(ArrayIndexOutOfBoundsException)

public class Runtime_02 {
	public static void main(String[] args) {
		int a[] = new int[5];
		for (int i = 0; i < 10; i++) {
			a[i] = i + 1;
			System.out.print(" " + a[i]);
		}
	}
}

这里写图片描述

**(3)数组元素类型不匹配异常(ArrayStoreException) **

public class Runtime_03 {
	public static void main(String[] args) {
		Object obj[] = new String[5];
		for (int i = 0; i < obj.length; i++) {
			obj[i] = new Integer(i);
			System.out.print(" " + obj[i]);
		}
	}
}

这里写图片描述
**(4)强制类型转换异常(ClassCastException) **

public class Runtime_04 {
	public static void main(String[] args) {
		Object x = new Integer(0);
		System.out.println((String) x);
	}
}

这里写图片描述
**(5)索引越界异常(IndexOutOfBoundsException) **

public class Runtime_05 {
	public static void main(String[] args) {
		int[] array = new int[5];
		for (int i = 0; i < array.length; i++) {
			array[i] = i;
		}
		for (int i = 1; i <= array.length; i++) {
			System.out.print(array[i - 1] + " " + array[i]);
		}
	}
}

这里写图片描述
**(6)空指针异常(NullPointerException) **

public class Runtime_06 {
	static Test test;
	public static void main(String[] args) {
		System.out.println(test.talk());
	}
}
class Test {
	public String talk() {
		return "this is a boy";
	}
}

这里写图片描述
**(7)数字格式转换异常(NumberFornatException) **

public class Runtime_07 {
	private String name;
	private String password;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public static void main(String[] args) {
		Runtime_07 r7 = new Runtime_07();
		String name = r7.getName();
		int password = new Integer(r7.getPassword()).intValue();
		System.out.println("用户名为" + name + ";密码为" + password);
	}
}

这里写图片描述
**(8)字符串索引越界异常(StringIndexOutBounds) **

public class Runtime_08 {
	public static void main(String[] args) {
		String str = "This is a StringIndexOutBounds";
		char ch = str.charAt(30);
		System.out.println(ch);
	}
}

这里写图片描述
操作错误(9)(UnsupportedOperationException)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Runtime_09 {
	public static void main(String[] args) {
		String[] listArray = new String[2];
		List list = Arrays.asList(listArray);
		List list1 = new ArrayList(list);
		list1.add("hello");
		listArray[0] = "china";
		list.add("world");		// 抛出操作错误异常
		System.out.println(list.get(0) + "," + list.get(1));
	}
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涛涛之海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值