(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));
}
}