Java中的异常处理

1:异常(Exception)的概念

  Java异常时Java提供的用于处理程序中错误的一种机制

2:处理方式

  Java是采用面向对象的方式来处理异常的。

  • 抛出异常:在执行一个方法是,如果发生异常,则这个方法生成代表该异常的一个对象,停止当前执行路径,并把异常对象提交给JRE
  • 捕获异常:JRE得到该异常后,寻找相应的代码来处理该异常,JRE在方法的调用栈查找,从生成的异常方法中开始回溯,直到找到相应的异常处理代码为止。

3:异常分类:

  JDK中定义了很多异常类,这些类对应了各种各样可能出现的异常事件,所有异常对象都是派生于Thorwable类的一个实例。如果内置的异常类不能够满足需要,还可以创建自己的异常类

  

  Error(错误)类 --- 不需要我们管 --- 唯一的办法:重启

  Exception 中    Checked Exception是编译器会检查的

           RuntimeException编译器不会检查,比如:

            1:int a = 1/0;  

            2: 空指针异常(NullPointerException):,一般是对象为空

            3:数组越界(ArrayIndexOutOfBoundsException)

            4:对象转化出错(ClassCastException)  可以想判断  e.g: obj instanceof

            5:数字格式异常(NumberFormatException)

              e.g  String str = "153153ag";

                 Integer i = new Integer(str);

4:异常的处理办法第一种:      捕获异常(try catch finally)

  try:

  • 一个try语句必须带有至少一个catch语句块或一个finally语句块..
  • 当异常处理的代码执行结束以后,是不会回到try语句去执行尚未执行的代码

  catch:

  • 每个try语句块可以伴随一个或多个catch语句,用于处理可能产生的不同类型的异常对象
  • 常用方法:

     toString()方法:显示异常的类名和产生异常的原因

     getMessage()方法: 只显示产生异常的原因,但不显示类名。

     PrintStackTrace()方法: 用来跟踪异常事件发生时堆栈的内容。

      -- 这些方法均继承自Throwable类

  • 如果异常类之间有继承关系,在顺序安排上需注意,越是顶层的类,越放在下面,再不然就直接把多余的catch省略掉

  finally

  • 通常在finally中关闭程序块已打开的资源,比如:文件流,释放数据库连接等。

  实例:

  文件操作:

    

   这个问题是因为文件可能不存在

 

     

代码:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class TestFileReader {

    public static void main(String args[]) {
        FileReader reader = null;
        try {
            reader = new FileReader("d:/lp.txt");
            char c = (char) reader.read();
            char c2 = (char) reader.read();
            System.out.println("" + c + c2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //这里的两个catch不能换位置  因为IOException是FileNotFoundException的父类
        catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
文件操作

 

  try - catch - finally - return 的执行顺序:    

  代码:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class TestFileReader {

    public static void main(String args[]) {
        String str = new TestFileReader().openFile();
        System.out.println(str);
    }

    private String openFile() {

        System.out.println("aaa");
        try {
            FileInputStream fis = new FileInputStream("d:/lp.txt");
            int a = fis.read();
            System.out.println("bbb");
            return "step1";
            
        } catch (FileNotFoundException e) {
            System.out.println("catching!!");
            e.printStackTrace();
            return "step2";
        } catch (IOException e) {
            e.printStackTrace();
            return "step3";
        }finally{
            System.out.println("finally");
        }
    }
    
}
无异常代码

 

  结果:

      

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class TestFileReader {

    public static void main(String args[]) {
        String str = new TestFileReader().openFile();
        System.out.println(str);
    }

    private String openFile() {

        System.out.println("aaa");
        try {
            FileInputStream fis = new FileInputStream("d:/wu.txt");
            int a = fis.read();
            System.out.println("bbb");
            return "step1";
            
        } catch (FileNotFoundException e) {
            System.out.println("catching!!");
            e.printStackTrace();
            return "step2";
        } catch (IOException e) {
            e.printStackTrace();
            return "step3";
        }finally{
            System.out.println("finally");
        }
    }
    
}
有异常代码

 

  结果:

  

  结论:1:执行try catch   给返回值赋值

      2:执行finally

      3:return 

 5:异常处理办法第二种:   throws字句

       

 

posted @ 2015-05-08 21:35  李_鹏  阅读(252)  评论(0编辑  收藏  举报