java运行异常_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();

}

}

}

434629ceef43c164d77b50f3e2ada74b.png

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

}

}

}

1f8b161fc06c5e84459fcd37bfed2d5c.png

**(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]);

}

}

}

744937191e1e73340833985265bf0adf.png

**(4)强制类型转换异常(ClassCastException) **

public class Runtime_04 {

public static void main(String[] args) {

Object x = new Integer(0);

System.out.println((String) x);

}

}

9be7d904531c613d023739c431a819c9.png

**(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]);

}

}

}

3a0d77070464c6e9f164244f1ea616b8.png

**(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";

}

}

e371280770e6416631044915987cf506.png

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

}

}

411e9896f7253cdd934773345c453c24.png

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

}

}

a628b1871ca56a5cdc1aa8a19815c0bf.png

操作错误(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));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值