异常处理
1、异常与错误
错误对于程序而言是致命的,运用java的异常处理机制,异常发生后经过处理,程序还是能正常运行的。如:数组越界异常、除数为0异常等。异常类是指Exception类及其子类。
抛出异常:发生异常会后,生成异常对象,并把对象提交给运行系统的过程。
异常对象:由程序本身或java虚拟机产生,一个异常产生一个异常对象。
2、异常分类:
运行时期的异常:编译时期没有问题,运行时产生错误。运行异常一旦发生,异常后面的代码就不能运行,需要修改代码,抛出的使RuntimeException类或者是他的子类。如:空指针异常、下标越界异常、类型强制转换异常。
非运行时期的异常:如:非法访问异常、类没有找到异常等。
public class ExceptionDemo {
public static void main(String[] args) {
int []a={
12,3,456,122};
int num=a[4];
System.out.println(num);
}
}
该数组的最大下标为3,但是要输出的数组的下表为4,所以抛出了数组越界异常:java.lang.ArrayIndexOutOfBoundsException
3、throw关键字
throw用在方法体内,是抛出一个异常对象,是抛出异常的具体实施。而throws只是对异常的声明,告诉调用则方法内部有异常,且没有处理该异常。
在异常类ArrayIndexOutOfBoundsException中,有如下构造方法去创建异常对象:
import java.util.Scanner;
public class ExceptionDemo {
public static void main(String[] args) {
System.out.println("请输入数组的下标");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
ExceptionDemo.num(a);
}
public static void num(int i) {
int[] a = { 12, 3, 456, 122 };
if (i >= a.length)
throw new ArrayIndexOutOfBoundsException("越界异常");
int num = a[i];
System.out.println(num);
}
}
4、throws关键字
当该异常并不需要产生异常的方法去处理,或者不知道如何处理,则用throws在方法头部用throws抛出异常,表名该方法没有对异常处理,由该方法的调用者处理。
5、try——catch——-finally语句捕获和处理异常
try catch finally是直接处理异常,处理完成之后程序继续往下执行。
格式:
try {
//需要被检测的语句。(可能会产生异常的代码)
}
catch(异常类 变量) { //异常的处理语句。 (捕获到异常后的处理代码)
}
finally {
//一定会被执行的语句。(无论是否捕获到异常均会执行)
}
数组越界异常的处理:
package pers.abb.trycatch;
import java.util.Scanner;
public class TryCatch {
public static void main(String[] args) {
System.out.println("请输入数组的下标");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
TryCatch.num(a);
}
public static void num(int i) {
int[] a = { 12, 3, 456, 122 };
try {
int num = a[i];
System.out.println(num);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组越界异常发生了。");
} finally {
a = null;// 内存垃圾清理
}
System.out.println("异常已经被处理了");
}
}
由运行结果可知,异常发生后,经过catch语句的处理,执行finally语句后,后面的System.out.println(“异常已经被处理了”);语句依旧能够正常运行。
6、自定义异常类
系统定义的异常都是系统可以预见的异常,但是在实际运用中,需要我们结合实际情况,创建适合自己程序的异常处理类。例如,人的年龄为负数的异常。
定义Person类:
package pers.zzz.Exception;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws AgeException {
//抛出异常给调用者,即main函数
if (age<0)
throw new AgeException(age);//异常发生,创建异常类对象
else this.age=age;
}
}
定义异常类:
package pers.zzz.Exception;
public class AgeException extends Exception{
int age;
public AgeException(int age){
this.age=age;
}
public void show(){
System.out.println("年龄为负数异常");
}
}
测试类:
package pers.zzz.Exception;
public class Test {
public static void main(String[] args) {
Person p=new Person();
try {
p.setAge(-11);
} catch (AgeException e) {
System.out.println("年龄定义有误");
e.show();
}
}
}
运行结果:
转载于//www.cnblogs.com/zhai1997/p/11362956.html
还没有评论,来说两句吧...