Java中的异常处理详解

异常处理的课堂笔记,希望对大家有用。http://blog.csdn.net/javaeeteacher谢谢!!!!1、什么是异常处理

异常:程序在运行过程中的一些特殊情况,例如:内存不够用,文件找不到,对象是Null,数组下标越界。当产生这些特殊情况的时候,可能会造成程序不能正常运行。

异常处理:让程序在发生异常的时候能够正常执行。

2、异常分类

检查性异常,必须处理,如果不处理,编译不能通过。

非检查性异常,可以不处理,但是一旦发生,程序就不能继续运行。

3、对于非检查性异常

虽然编译的时候不会报错,能够运行。但是运行的时候,如果发生错误,后果很严重。所以还是要处理。可以通过增加代码来解决。

例子:对于NullPointerException,在调用方法之前可以检查这个对象是否为null

if(date!=null){

date.getTime();

}

例子:a/b(b为0产生异常),代码中可以检查b是否为0

if(b!=0)

a/b;

else

// 抛出异常!

也可以采用try-catch进行处理。

4、try…catch对异常处理

准备工作:在进行异常处理之前,必须清楚可能会发生哪些异常,异常与要调用的方法有关,通常在方法的定义中通过throws声明可能发生的异常。

把可能产生异常的代码放在try语句中;

针对每种可能出现的异常,编写一个catch,catch中写出异常类型;

在catch对应的代码中编写对异常进行处理的代码,当异常产生并与当前异常类型匹配,将执行catch中的代码。

基本结构:

try{

// 可能产生异常的代码

}catch(异常类型异常对象){

// 异常产生时候的处理代码

}

注意:

每种类型的异常都必须有对应的catch,所有的异常都应该处理。

可以使用某个父类异常匹配多个子类异常。例如:catch(Exception e){}

如果异常类型之间有继承关系,子类一定要在前面。

如果希望某个变量在try和catch中都可以使用,则应该把变量的定义放在try语句之前。

例子:

package ch11;

import java.io.IOException;

public class Test {

public static void main(String[] args) {

String str1 = new String(“111a”);

str1 = “22”;

String str2 = “456”;

// 把可能产生异常的代码写在try语句中

try{

int x = Integer.parseInt(str1);

int y = Integer.parseInt(str2);

System.in.read();

// 当产生异常的时候,try语句中异常代码之后的代码不会执行

System.out.println(x+y);

// return ;

}catch(NumberFormatException e){ // 根据可能会出现的异常,写Catch,每种

类型的异常可以写一个Catch,Catch可以有多个,可以用父类异常匹配多个子类异常

// 如果多个Catch中的异常类型有继承关系,子类要写在前面

// 如果匹配上某种类型的异常,将执行catch中的代码

System.out.println(e.getMessage());

System.out.println(“———————————“);

System.out.println(e.toString());

System.out.println(“———————————“);

e.printStackTrace();

}catch(IOException e){

}

// 如果try语句没有给出catch,则必须给出finally,不管是否产生异常,

finally中代码都将执行,如果给出catch,也可以给出finally

finally{

System.out.println(“程序继续运行!”);

}

//System.out.println(new Test().check());

}

public boolean check() throws IOException{

String str = “adasf”;

System.in.read();

int a = Integer.parseInt(str);

return true;

}

}

5、获取异常信息

当产生异常的时候,会匹配上某个catch,系统会把异常对象赋值给catch中的变量,在catch代码块中可以访问异常信息,通过如下方法:

getMessage() 得到异常的最简单的描述

toString() 得到比较详细的异常信息

printStackTrace() 会把异常信息输出在默认的输出设备中。

例如:

try{

// 可能产生异常的代码

}catch(Exception e){

System.out.println(e.toString());

System.out.println(e.getMessage());

e.printStackTrace();

}

6、finally

如果希望某些代码一定执行,不管是否发生异常,可以把这些代码放在finally中,与try一起使用,

基本结构

try{

// 代码块

}catch(异常1 e1){

}catch(异常2 e2){

}finally{

// 一定要执行的代码

}

即使在try或者catch中有return语句,finally也一定会执行,在return之前执行。

7、异常的抛出

异常通常在方法中抛出,通过throw关键字,例如:throw new MyException(“sds”);

如果方法中有抛出异常的代码,则方法的声明中应该使用throws声明异常的类型,例如:

public void print() throws MyException{

throw new MyException(“sdfs”);

}

8、创建自己的异常类

继承Exception,实现构造方法即可。

package ch11;

public class MyException extends Exception {

public MyException(){

}

public MyException(String args){

super(args);

}

}

9、例子

package ch11;

public class Exercise {

public static void main(String[] args) {

int a[] = {1,3,4};

boolean hasException = false;

try{

a[3] = 4;

// hasException = false;

}catch(Exception e){

System.out.println(“数组越界!”);

hasException = true;

}finally{

if(hasException){

System.out.println(“程序异常运行结束!”);

}else{

System.out.println(“程序正常运行结束!”);

}

}

Exercise e = new Exercise();

try{

e.setAge(1000);

}catch(MyException ee){

System.out.println(ee.getMessage());

}

}

public void setAge(int age) throws MyException{

if(age<0 || age>120){

throw new MyException(age+”不是一个合理的年龄值!”);

}

}

}

http://blog.csdn.net/javaeeteacher

不要因为生活琐事而烦恼,不要因为儿女情长而忧愁,

Java中的异常处理详解

相关文章:

你感兴趣的文章:

标签云: