异常
程序在运行过程中出现的意外情况,java语言称之为异常。
程序设计时应在异常发生时进行处理,使程序不会因为异常而停止运行。
处理潜在异常的方法时有两种选择:1.在调用可能引起异常的方法时,捕获并处理异常;2.声明该方法可能会抛出该异常。
java.lang.Throwable java中异常的管理 分为 Error 和 Exception 两大类
Error类代表非常严重的系统错误,一般是jvm(java虚拟机)内部出了问题,如内存不足等,一般与代码编写者无关。
Exception类代表程序有可能恢复的异常情况。是整个java异常情况的父类。分为RuntimeException类及其所有子类和非RuntimeException子类的异常类。
其中RuntimeException类及其子类属于程序缺陷造成的异常,是设计或实现的问题。不强制程序员必须处理。这类异常也被称为非受检异常,几种常见:
NullPointerException - 空指针引用异常
ClassCastException - 类型强制转换异常。
IllegalArgumentException - 传递非法参数异常。
ArithmeticException - 算术运算异常
ArrayStoreException - 向数组中存放与声明类型不兼容对象异常
IndexOutOfBoundsException - 下标越界异常
NegativeArraySizeException - 创建一个大小为负数的数组错误异常
NumberFormatException - 数字格式异常
SecurityException - 安全异常
UnsupportedOperationException - 不支持的操作异常
http://blog.csdn.net/qq635785620/article/details/7781026
非RuntimeException类及其子类是程序外部问题引起的异常,如果不进行处理,会有语法错误,这类异常被称为受检异常。
异常的处理
抛出异常、捕获异常、处理异常。
try块:将一个或多个语句放入try块,表示这些代码可能会抛出异常,需要jvm在执行这段代码时特殊监控。
catch块:当try块代码出现异常时,让jvm逐个去匹配,交由第一个匹配的catch块处理。把异常子类catch块写在前面,异常父类catch块写在后面。
finally块:无论try块代码有没有异常,finally块代码一定会执行。finally块内经常放一些释放资源的代码,
public class HandleExceptionTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number=0;
try{
// String str=args[0];
// number=Integer.parseInt(str);
String str="zz";
number=Integer.parseInt(str);
System.out.println("你输入的数字为"+number);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("发生ArrayIndexOutOfBoundsException:没有命令行参数");
}catch(NumberFormatException e){
System.out.println("发生NumberFormatException:非法的数字");
}finally{
System.out.println("finally代码块");
}
}
}
运行结果:
发生NumberFormatException:非法的数字
finally代码块
输出异常信息
Throwable类中定义了三个属性来存储该异常的相关信息。
message: String类型 描述该异常的详细描述性文本信息。
StackTrace:StackTraceElement[]类型 描述引发该异常的所有方法的调用记录。最常用“void printStackTrace()”方法
cause:Throwable类 描述产生该异常的原因。
public class HandleExceptionTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number=0;
try{
// String str=args[5];
// number=Integer.parseInt(str);
String str="zz";
number=Integer.parseInt(str);
System.out.println("你输入的数字为"+number);
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(e.getCause());
System.out.println("发生ArrayIndexOutOfBoundsException:没有命令行参数");
}catch(NumberFormatException e){
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(e.getCause());
System.out.println("发生NumberFormatException:非法的数字");
}finally{
System.out.println("finally代码块");
}
}
}
运行结果:
java.lang.NumberFormatException: For input string: "zz"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at lesson012.HandleExceptionTest.main(HandleExceptionTest.java:12)
For input string: "zz"
null
public class HandleExceptionTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number=0;
try{
String str=args[5];
number=Integer.parseInt(str);
// String str="zz";
// number=Integer.parseInt(str);
System.out.println("你输入的数字为"+number);
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(e.getCause());
System.out.println("发生ArrayIndexOutOfBoundsException:没有命令行参数");
}catch(NumberFormatException e){
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(e.getCause());
System.out.println("发生NumberFormatException:非法的数字");
}finally{
System.out.println("finally代码块");
}
}
}
运行结果:
java.lang.ArrayIndexOutOfBoundsException: 5
at lesson012.HandleExceptionTest.main(HandleExceptionTest.java:9)
5
null
发生ArrayIndexOutOfBoundsException:没有命令行参数
finally代码块
异常栈跟踪
java运行中一系列方法的调用称为方法调用栈。
异常沿着调用栈上传的过程中,异常对象会维护一个称为栈追踪的结构。
栈追踪会记录未处理异常的各个方法,以及发生问题的代码行。
声明异常
在方法体后面 加上 throws 异常类型,异常类型
如 public void h(int) throws IOException,OtherException
如果子类重载父类中声明抛出异常的方法,子类中可以不抛或者抛异常的子类。
手动抛出异常
public class ThrowExceptionTest {
public static void main(String[] args) {
ThrowExceptionTest test=new ThrowExceptionTest();
System.out.println(test.createDoubleArray(-12).length);
}
private double[] createDoubleArray(int length) {
if(length<0){
// throw new NegativeArraySizeException("数组长度不能小于0"); //抛出异常对象
throw new NullPointerException("数组长度不能小于0"); //抛出异常对象 后面为 非受检异常
}
return new double[length];
}
}
运行结果:
Exception in thread "main" java.lang.NullPointerException: 数组长度不能小于0
at lesson012.ThrowExceptionTest.createDoubleArray(ThrowExceptionTest.java:13)
at lesson012.ThrowExceptionTest.main(ThrowExceptionTest.java:7)
自定义异常
public class MyException extends Exception{
/*Throwable类的四种构造方法*/
public MyException(Throwable cause) {
super(cause);
}
public MyException(String message,Throwable cause){
super(message,cause);
}
public MyException(String message){
super(message);
}
public MyException(){}
}
public class MyExceptionTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyExceptionTest test=new MyExceptionTest();
try{
System.out.println(test.creatDoubleArray(-10).length);
}catch(MyException ex){
ex.printStackTrace(); //用try 和 catch代码块处理
}
}
private double[] creatDoubleArray(int i) throws MyException{ //声明异常
// if(i<0){
// throw new MyException("数组长度小于0"); //手动抛出异常
// }
return new double[i];
}
}
运行结果:
Exception in thread "main" java.lang.NegativeArraySizeException
at lesson012.MyExceptionTest.creatDoubleArray(MyExceptionTest.java:20)
at lesson012.MyExceptionTest.main(MyExceptionTest.java:9)
public class MyExceptionTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyExceptionTest test=new MyExceptionTest();
try{
System.out.println(test.creatDoubleArray(-10).length);
}catch(MyException ex){
ex.printStackTrace(); //用try 和 catch代码块处理
}
}
private double[] creatDoubleArray(int i) throws MyException{ //声明异常
if(i<0){
throw new MyException("数组长度小于0"); //手动抛出异常
}
return new double[i];
}
}
运行结果:
lesson012.MyException: 数组长度小于0
at lesson012.MyExceptionTest.creatDoubleArray(MyExceptionTest.java:18)
at lesson012.MyExceptionTest.main(MyExceptionTest.java:9)
try-with-resources语句
try-with-resources这种声明方式指定了一个或多个资源,而且这些资源需要在程序结束的时候进行关闭。这种方式可以确保每个指定的资源都可以在声明结束的时候进行关闭(就是在try(){}结束的时候)。但是前提是这些资源必须实现接口java.lang.AutoCloseable(其中包括实现了java.io.Closeable接口的所有对象),原因是java.io.Closeable接口继承了java.lang.AutoCloseable接口。
可以catch多个异常,然后写上处理语句