------- android培训、java培训、期待与您交流! ----------
一、 异常概念。
异常就是程序在运行中的发生的不正常情况。java中的异常分为编译时异常和运行时异常,编译时需要检测的异常叫做编译时异常;编译时不需要检测的异常叫做运行时异常。编译时异常需要我去处理,而运时异常我们可以不处理,报给虚拟机后程序结束,就是Exception的子类RuntimeException和RuntimeException的子类。
java中异常分为两类,一类是Error;另一类是Exception。
而Error是错误的类型,无法处理的异常类型。而Exception是可处理的异常。如NoClassDefFoundError,IOError等等,凡是以Error结尾的异常都是 Error异常。以Exception结尾的异常是可处理的异常。如ClassNotFoundException,ArrayIndexOutOfBoundsException等等。
二、 异常对象。
在java中,把各种各样的异常进行分类封装后,就形成了异常对象。换句话说就异常就是封装着程序在编译或运行时的不正常行为,以面向对象的思惟来使用异常。
三、 捕获异常。
在java中,捕获异常要使用到异常捕获代码块,即trycatch finally。如下:
例一:
class Try$Catch
{
public static void main(String[] args)
{
int[] arr=new int[5];
try
{
int a=arr[5];
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
}
四、 抛出异常。
在java中异常中,如果我们不能处理的异常,那我们就要把异常抛给调用者,由调用者去处理,有两种情况,第一种:如果发生的异常无法处理,那就抛出RuntimeException的类型异常。不需要我们处理,由虚拟机捕获取,中断程序。第二种是需要调用者去捕获后处理,那就要抛出相应的异常,并在方法中声明异常类型列表,抛出多个用逗号分开。代码如下:
例二:
class Try$Catch1
{
public static void main(String[] args)
{
int v=0;
try
{
int a=10/v;
}
catch (ArithmeticException e)
{
throw new ArithmeticException("除数不能为0");
}
}
}
例三:
class Try$Catch1
{
public static void main(String[] args)
{
try{
method(10);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
public static void method(int index) throws ArrayIndexOutOfBoundsException
{
int[] arr=new int[5];
if(index>arr.length-1)
throw ArrayIndexOutOfBoundsException("角标越界");
int a=arr[index];
}
}
五、 异常的finally
finally是异常中的一部份,不管异常是否发生,程序在最后一定会执行finally的代码块。即使程序在遇到return,也去执行finally代码块。代码如下:
例四:
class Try$Catch1
{
public static void main(String[] args)
{
try{
//method(10);
method(4);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
finally
{
System.out.println("finally被执行了")
}
}
public static void method(int index) throws ArrayIndexOutOfBoundsException
{
int[] arr=new int[5];
if(index>arr.length-1)
throw ArrayIndexOutOfBoundsException("角标越界");
int a=arr[index];
}
}
运行结果:
当method(10)时
finally被执行了
当method(4)时
finally被执行了
例五:
class Try$Catch1
{
int test=-2;
public static void main(String[] args)
{
System.out.println(method(4));
}
public static int method(int index)
{
int[] arr=new int[5];
try{
test= arr[index];
return test
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
test=-1;
return test;
}
finally
{
test=100;
System.out.println("finally被执行了");
}
}
}
执行结果:
finally被执行了
0
结论:可以看出,当有返回语句return时,在执行了finally之后才返回,而且无法改为返回值。
例六:
try{}finally{}组合时,需要在方法声明抛出异常。如下:
class Try$Catch1
{
public static void main(String[] args)
{
System.out.println(method(4));
}
public static int method(int index)
{
try
{
Thread.sleep(10);//此方法抛出InterruptedException
}
finally
{
} }
}
编译上述代码发生异常:如下:
Try$Catch1.java:11: 未报告的异常 java.lang.InterruptedException;必须对其进行捕
捉或声明以便抛出
Thread.sleep(10);//此方法抛出InterruptedException
^
2 错误
以上代码必须声明异常抛出。代码如下:
class Try$Catch1
{
public static void main(String[] args)
{
System.out.println(method(4));
}
public static int method(int index) throws InterruptedException
{
try
{
Thread.sleep(10);//此方法抛出InterruptedException
return 1;
}
finally
{
}
return 0;
}
}
注意:finally代码块之有一个。
finally的作用:
1) 资源释放。如在lock中,当锁中的代码发生错误时,以用业解锁。
2) 连接数据库时,当查询数据时,发生异常,用于关闭数据库。
3) 总之,主要用于资源释放
六、 多个catch.
在异常捕获处理代码块中可以有多个catch,但必须有一定的顺序,否则编译错误。如下
class Try$Catch1
{
public static void main(String[] args)
{
System.out.println(method(4));
}
public static int method(int index)
{
try
{
Thread.sleep(10);//此方法抛出InterruptedException
return 1;
}
catch(InterruptedException e)
{
}
catch(Exception e)
{
}
finally
{
}
return 0;
}
}
从子类开始,依次到直属父类,最后到超类。也就是异常根类Exception。如下代码在编译时出错:
class Try$Catch1
{
public static void main(String[] args)
{
System.out.println(method(4));
}
public static int method(int index)
{
try
{
Thread.sleep(10);//此方法抛出InterruptedException
return 1;
}
catch(Exception e)
{
}
catch(InterruptedException e)
{
}
finally
{
}
return 0;
}
}
Try$Catch1.java:18: 已捕捉到异常 java.lang.InterruptedException
catch(InterruptedException e)
^
1 错误
七、 异常处理原则。
1) 在函数内部中,如有抛出需要检测的异常的代码是,那么必须在函数上声明。
2) 如果调用了声明异常的函数,要么cry…catch,要么throw,否则编译失败。
3) 什么时候使用异常,什么时候throws。当功能内容可以解决时,用try..catch。当解决不了时,用thorws告诉调用者,由调用者去解决。
4) 一个功能如果抛出异常,那么调用时,必须有对应的try…catch进行针对性的处理;内部有几个需要检测的异常,就抛出几个异常,抛出几个就用几个catch。
八、 异常的特点。
1) try{}catch{}finally{}。
2) try{}catch{}catch{}….{}finally{}//多个catch。
3) try{}finally{}。
九、 使用异常注意事项。
1) 子类在覆盖父类的方法时,父类的方法如果抛出异常时,那么子类的方法只能抛出父类的异常或异常子类,也可以不抛出。
2) 如果父类抛出多个异常,那么子类只能抛出父类异常的子集。
3) 如果父类的方法没有抛出异常,那么子类覆盖时,绝对不能抛出任可异常。