现在的位置: 首页 > 综合 > 正文

浅述异常处理

2013年10月29日 ⁄ 综合 ⁄ 共 2941字 ⁄ 字号 评论关闭
文章目录

今天上课讲的异常,自己整理一下,留用。。。

异常就是在程序的运行过程中所发生的不正常的事件,它会中断正在运行的程序。Java的异常处理是通过5个关键字来实现的:try、catch、 finally、throw、throws。

1.try:执行可能产生异常的代码;
2.catch:捕获异常;
3.finally:无论是否发生异常,此处代码总是执行;
4.throw:手动抛出异常;
5.throws:声明方法可能要抛出的各种异常 。

1.使用try/catch块捕获异常,分为三种情况:

1)第一种情况:

public class AccpException {
public static void main(String[] args) {
System.out.print("请输入课程代号(1至3之间的数字):");
Scanner in = new Scanner(System.in);
try {
int courseCode = in.nextInt();
switch (courseCode) {
	case 1:
	        System.out.println("C#编程");
	        break;
	case 2:
	        System.out.println("Java编程");
	        break;
	case 3:
	        System.out.println("SQL基础");
}
} catch (Exception ex) {
        System.out.println("输入不为数字!");
}
System.out.println("欢迎提出建议!");
}
}

2)第二种情况:

public class AccpException {
public static void main(String[] args) {
System.out.print("请输入课程代号(1至3之间的数字):");
Scanner in = new Scanner(System.in);
try {
int courseCode = in.nextInt();
switch (courseCode) {
	case 1:
		System.out.println("C#编程");
		break;
	case 2:
		System.out.println("Java编程");
		break;
	case 3:
		System.out.println("SQL基础");
}
} catch (Exception ex) {
        System.out.println("输入不为数字!");
        ex.printStackTrace();
}
System.out.println("欢迎提出建议!");
}
}

InputMismatchException:输入不匹配异常。

3)第三种情况:

public class AccpException2 {
public static void main(String[] args) {
System.out.print("请输入课程代号(1至3之间的数字):");
Scanner in = new Scanner(System.in);
try {
int courseCode = in.nextInt();
switch (courseCode) {
	case 1:
	        System.out.println("C#编程");
	        break;
	case 2:
	        System.out.println("Java编程");
	        break;
	case 3:
	        System.out.println("SQL基础");
}
} catch (NullPointerException ex) {
        System.out.println("输入不为数字!");
}
System.out.println("欢迎提出建议!");
}
}

InputMismatchException:输入不匹配异常。

解析:

java.util.InputMismatchException----------异常类型
at java.util.Scanner.throwFor(Scanner.java:840)----------抛出异常的方法
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at s2jsp.sg.ch05.AccpException.main(AccpException.java:23)

4)多重catch块 

一段代码可能会引发多种类型的异常
当引发异常时,会按顺序来查看每个 catch 语句,并执行第一个与异常类型匹配的catch语句
执行其中的一条 catch 语句之后,其后的 catch 语句将被忽略 

public void method(){
try {
     // 代码段
     // 产生异常(异常类型2)
} catch (异常类型1 ex) {
     // 对异常进行处理的代码段
} catch (异常类型2 ex) {
     // 对异常进行处理的代码段
} catch (异常类型3 ex) {
     // 对异常进行处理的代码段
}
// 代码段
}

2.抛出异常

如果在当前环境无法解决一个异常,就要将其抛出,把异常交给调用者处理。

在不使用try、catch语句或throws的前提下,只可以抛出运行时异常对象RuntimeException或其子类对象,如:NullPointerException、IllegalArgumentException等。

public class AccpTeacher {
    private String id;     // 教员编号,长度应为7
    public void setId(String pId){
 // 判断教员编号的长度是否为7
 if (pId.length() == 7) {
id = pId;
 } else {
throw new IllegalArgumentException("参数长度应为7!");
 }
    }
}

3.声明异常

public class AccpTeacher {
    private String id;// 教员编号,长度应为7
    public void setId(String pId) throws IllegalArgumentException{
 // 判断教员编号的长度是否为7
 if (pId.length() == 7) {
id = pId;
 } else {
throw new IllegalArgumentException("参数长度应为7!");
 }
    }
}

4.JDK类库中的异常分类

5.常见异常

ArithmeticException:数学异常
int a=12 / 0;  //抛出ArithmeticException
NullPointerException:空指针异常
Date d= null;
System.out.println(d.toString());  //抛出NullPointerException
ArrayIndexOutOfBoundsException:下标越界异常
int[] array=new int[4];
array[0]=1;   
array[7]=1;  //抛出ArrayIndexOutOfBoundsException
ClassCastException:类型转换异常:
Animal animal=new Dog();
Cat cat=(Animal)animal;

参考资料:

刘老师的课件。。。

抱歉!评论已关闭.