JAVA常见异常出错 及原因

1. 

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at com.Demo_4.Demo_Error.TestArithmetic(Demo_Error.java:10)
	at com.Demo_4.Demo_Error.main(Demo_Error.java:5)

报错原因:  进行除0操作    分母不能为0

2.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
	at com.Demo_4.Demo_Error.TestArrayIndexOutOfBounds(Demo_Error.java:19)
	at com.Demo_4.Demo_Error.main(Demo_Error.java:6)

报错原因:   数组下标越界    数组长度不够

3.

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Short
	at com.Demo_4.Demo_Error.TestArrayStore(Demo_Error.java:27)
	at com.Demo_4.Demo_Error.main(Demo_Error.java:7)

报错原因:   数组元素类型不匹配

4.

Exception in thread "main" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
	at com.Demo_4.Demo_Error.TestClassCast(Demo_Error.java:34)
	at com.Demo_4.Demo_Error.main(Demo_Error.java:8)

报错原因:  强制类型转换异常

5.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 11
	at java.lang.String.charAt(Unknown Source)
	at com.Demo_4.Demo_Error.TestIndexOutOfBounds(Demo_Error.java:41)
	at com.Demo_4.Demo_Error.main(Demo_Error.java:9)

报错原因:  字符串下标越界

6.

Exception in thread "main" java.lang.NullPointerException
	at com.Demo_4.Demo_Error.TestNullPointer(Demo_Error.java:49)
	at com.Demo_4.Demo_Error.main(Demo_Error.java:12)

报错原因:  没有实例化对象类

7.

Exception in thread "main" java.lang.NumberFormatException: For input string: "大法师"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.<init>(Unknown Source)
	at com.Demo_4.Demo_Error.TestNumberFornat(Demo_Error.java:54)
	at com.Demo_4.Demo_Error.main(Demo_Error.java:13)

报错原因: 数字格式转换异常


以下是异常报错代码

package com.Demo_4;

public class Demo_Error {
	static DemoClass s;
	public static void main(String[] args) {
		//TestArithmetic();
		//TestArrayIndexOutOfBounds();
		//TestArrayStore();
		//TestClassCast();
		//TestStringIndexOutOfBounds();
		//TestNullPointer();
		//TestNullPointer();
		TestNumberFornat();
	}
	//发生异常的原因为  分母不能为0   
	public static void TestArithmetic(){
		int result = 0;
		for (int i = 0; i <10; i++) {
			result = i+1/i;
			System.out.print(result+"\t");
		}
	}
	//数组下标长度超出
	public static void TestArrayIndexOutOfBounds(){
		int array[]  = new int [7];
		for (int i = 0; i < 10; i++) {
			array[i] = i+2;
			System.out.print(array[i]+"\t");
		}
	}
	//数组元素类型不匹配
	public static void TestArrayStore(){
		Object array[]  = new String [7];
		array[1] = new Short((short) 20);
		System.out.println(array[1]);
	}
	//强制类型转换异常
	public static void TestClassCast(){
		Object s = new Double(20);
		System.out.println((Integer)s);
	}
	//字符串下标越界    
	public static void TestStringIndexOutOfBounds(){
		String word = "hello word";
		System.out.println("word的长度为:"+word.length());
		System.out.println("取word长度加1"+word.charAt(word.length()+1));
	}
	//没有实例化对象类
	public static void TestNullPointer(){
		System.out.println(s.getColor());
	}
	//数字格式转换异常
	public static void TestNumberFornat(){
		String pw = "大法师";
		int password = new Integer(pw);
		System.out.println(password);
	}
}
class DemoClass{
	public  String getColor(){
		return "黄色";
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值