异常
异常:异常机制的出现是为了提高代码的健壮性
异常的继承关系:
1.Throwable是Java中所有错误和异常类的父类(万事万物皆可抛)
2.Error,错误类,必须通过修改代码解决,一旦出现错误,系统会自动生成一个错误对象,进行提示!
3.Exception,异常类,对异常进行处理!
测试阶段
package com.exception;
public class Main {
public static void main(String[] args) {
int[] a= {3,6,12};
int[] b= {2,1};
int[] c=null;
//获取数组中第三个元素
// System.out.println(a[2]);
// System.out.println(third(a));
// System.out.println(third1(b));
//注意 遇见异常,两种解决方式
//1.继续往上面抛异常
//2.try..catch 处理捕捉异常
try {
//try里面放的是 可能会出现异常的代码
System.out.println(third2(a));
} catch (Exception e) {
//异常路径查看
e.printStackTrace();
System.out.println("会不会执行呢???");
}
int[] d= {1,11,13,14};
try {
third2(b);
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}finally {
//注意!此部分可写可不写!!!不管异常代码如何运行
//此处代码必走!!!
System.out.println("无论如何都会执行的!!");
}
try {
third3(c);
} catch (ZeroException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//定义函数 测试异常
//1.缺点:没有检测 传入的数组到底有没有第三个元素
public static int third(int[] array) {
return array[2];
}
//2.缺点:返回值不准确
public static int third1(int[] array) {
if(array.length>=3) {
return array[2];
}else {
return 0;
}
}
//3.抛出一个异常对象,就必须在方法名中声明异常类型
//缺点:虽然避免报错,并且也抛出了异常.但是不够具体
public static int third2(int[] array) throws Exception {
if(array.length>=3) {
return array[2];
}else {
//没有第三个元素 抛出异常(本质:就是创建一个异常对象 并返回)
Exception e=new Exception("没有第三个元素");
//注意注意!!返回异常,从来都不用return
throw e;
}
}
public static int third3(int[] array) throws ZeroException {
if(array==null) {
NullPointerException e=new NullPointerException("数组不存在");
throw e;
}
if(array.length<3) {
ArrayIndexOutOfBoundsException e=new ArrayIndexOutOfBoundsException("数组中没有第三个元素");
throw e;
}
return array[2];
}
}
系统提供的异常:
1.SQLException 数据库SQL异常
2.IOException 输入输出异常
3.AWTException 抽象窗口工具异常
5.ClassNotFoundException 类不存在异常
4.(关键)RuntimeException 运行时异常
- a.NullPointException 空指针异常
- b.IllegalStateException 非法状态异常
- c.ArithmeticException 算术逻辑异常
- d.ClassCastException 类型转换异常
- e.IllegalArgumentException 非法参数异常
- f.ArrayStoreException 数组元素和数组类型不匹配的异常
- g.IndexOutOfBoundsException 数组越界异常
- h.NegativeArraySizeException 数组元素个数为负的异常
异常的分类:
1.检查型异常(checked):除了RuntimeException及其子类,其他的异常都是检查型异常
特点:强制要求对检查型异常进行处理,不处理就会报错
2.非检查型异常(运行时异常)unchecked:所有的运行时异常及其子类
都不是检查型异常
特点:非检查型异常,不要求强制处理!只有在代码运行以后,如果有错误,运行时异常才会出现!!!
throw和throws的区别:
.throw,写在方法内部,用于返回一个异常对象
2.throws,写在方法声明中,后面跟上返回的异常类型(如果返回异常类型
较多,也可以返回他们的父类);标明该方法可能出现异常,请使用者自行处理!!!抛出异常
final和finally的区别
1.final是一个修饰关键字
- 可以修饰类–不能再被继承
- 可以修饰方法–不能被子类重写,但不影响调用
- 修饰变量–不能再被修改值
2.finally 只是try…catch环节中的一个步骤!可有可无
注意下边两个是不一样的
// int[] a=null;在内存当中没有空间没有值
// int[] b= {};在内存中有空间但是没有值
所以以Third3为例可以检查出int[] a=null;
但是检查不出来int[] b={};所以要自定义一个异常
if(array==null) {
NullPointerException e=new NullPointerException("数组不存在");
throw e;
}
自定义一个异常如下
package com.exception;
public class ZeroException extends Exception {
//由父类决定异常的类型!!!
//构造方法
public ZeroException() {
}
//带参数的构造 来自父类
public ZeroException(String message) {
super(message);
}
}
所以third方法可以这样写
public static int third3(int[] array) throws ZeroException {
// int[] a=null;
// int[] b= {};
if(array==null) {
NullPointerException e=new NullPointerException("数组不存在");
throw e;
}
if(array.length<3) {
ArrayIndexOutOfBoundsException e=new ArrayIndexOutOfBoundsException("数组中没有第三个元素");
throw e;
}
if(array.length==0) {
ZeroException z=new ZeroException("数组中没有元素");
throw z;
}
return array[2];
}
异常自己多打打代码就可以理解了,学语言不理解的地方自己多打打代码就容易理解了.这就我学习Java的方法.贵在坚持