Java常见异常和解决办法
1、java.lang.ArithmeticException
算术运算异常,因为除数为0,所以引发了算数异常
2、Java.lang.StringIndexOutOfBoundsException: String index out of range: -1
这是截取字符串substring()产生的下标越界异常。原因是可能是字符串为空,或长度不足1
3、java.lang.NullPointerException空指针异常
出现该异常的原因在于某个引用为null,但却调用了它的某个方法,这时就会出现该异常
4、ClassCastException
类型强制转换异常,例如:Object x = new Integer(0);System.out.println((String)x);
5、IllegalArgumentException
传递非法参数异常,此异常表明向方法传递了一个不合法或不正确的参数。你看看传值的方法是否参数不正确
6、NumberFormatException
数字格式异常,例如:把"176//240"这个输入字条转换为一个数字
7、ClientAbortException: java.io.IOException异常
原因是由于服务器正在处理http请求,正在输出内容时,用户关闭了浏览器,造成了ClientAbortException异常。它属于I/O异常中比较常见的一种。
8、ClientAbortException Caused by: java.NET.SocketException: Connection reset by peer: socket write error
这种异常已比较常见,通常有以下几种情况:
服务器的并发连接数超过了其承载量,服务器会将其中一些连接Down掉;客户关掉了浏览器,而服务器还在给客户端发送数据
9、ArrayStoreException
向数组中存放与声明类型不兼容对象异常,例如:Object x[] = new String[3];x[0] = new Integer(0);
10、NegativeArraySizeException
创建一个大小为负数的数组错误异常,例如int[] arr = new int[10];int i = arr[-1];
11、SecurityException
安全异常,例如:Android的权限异常,运行java的程序提示Missing requited Permissions manifest attribute in main jar等
12、UnsupportedOperationException
不支持的操作异常,例如String testStr = "100,300,400,545,666";List<String> test = Arrays.asList(testStr.split(","));test.remove("100");使用Arrays.asList()方法一定要注意类型的转换。
Java中的异常分为两大类:
1.Checked Exception(非Runtime Exception)
2.Unchecked Exception(Runtime Exception)
运行时异常
RuntimeException类是Exception类的子类,它叫做运行时异常,Java中的所有运行时异常都会直接或者间接地继承自RuntimeException类。
Java中凡是继承自Exception,而不继承自RuntimeException类的异常都是非运行时异常。附一张java异常机制图
原文地址:http://blog.****.net/qq_17168031/article/details/51433849