JAVA异常处理机制(一)

异常处理机制try-catch
语法定义:
try{
//可能出现异常的代码片段
}catch(XXXException e){
//捕获try中出现的XXXException后的处理操作代码
}
try-catch演示

public class Try_CatchDemo {
    public static void main(String[] args) {
        System.out.println("start");

        try{
        //String str = null;
        //System.out.println(str.length());
        //String str1 = "";
        //System.out.println(str1.charAt(0));

        String str2 = "a";
        System.out.println(Integer.parseInt(str2)); //NumberFormatException 数字格式异常


        /*
         * 在try块中出错语句以下的代码都不会被执行
         */
        System.out.println("!!!!!!");
        }catch(NullPointerException e){
            System.out.println("出现了空指针!");

        }catch(StringIndexOutOfBoundsException e){
            System.out.println("字符串下标越界了!");

        /*
         * 应当有一个好习惯,在最后一个catch处捕获Exception,防止一个未捕获的异常导致程序中断
         */
        }catch(Exception e){
            System.out.println("有异常");  
        }



        System.out.println("end");
    }
}

finally块

finally块是定义在异常处理机制的最后一块.
它可以直接跟在try后面或者最后一个catch后面.
finally可以保证只要程序执行到了try块中,那么无论try块中的语句是否会抛出异常,
finally都确保其内容一定被执行.通常会将释放资源等操作放在finally中
如流的关闭操作

public class FinallyDemo {
    public static void main(String[] args) {
        System.out.println("程序开始");
        try {
            String str = null;
            System.out.println(str.length());
        } catch (Exception e) {
            System.out.println("出错了!");
        }finally{
            System.out.println("finally!");
        }

        System.out.println("程序结束");
    }
}

finally在IO中的使用

public class FinallyDemo2 {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("fos.dat");
            fos.write(1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

上一串代码中close操作过于繁琐
不过JDK7之后推出了一个新特性:自动关闭
java中实现了AutoCloseable接口的类,都可以被该特性自动关闭

public class AutoCloseable {
    public static void main(String[] args) {
        try(
            FileOutputStream fos = new FileOutputStream("fos.dat");    //在try(){}  小括号中创建的对象将会被自动关闭
                ){
            fos.write(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

finally常见面试题
请分别说明final,finally,finalize

final用于声明属性,方法和类,分别表示属性不可交变,方法不可覆盖,类不可继承。
finally是异常处理语句结构的一部分,表示总是执行。
finalize是Object定义的方法,该方法是被GC执行的方法,
当一个对象即将被GC释放前,GC会调用该方法. 调用该方法完毕后意为着该对象被释放

public class FinallyDemo3 {
    public static void main(String[] args) {
        System.out.println(
                test("0")+","+test(null)+","+test("")       //3,3,3  程序执行到finally后将之前的结果覆盖,其实下面的代码是不科学的
                );
    }

    public static int test(String str){
        try {
            return str.charAt(0)-'0';
        } catch (NullPointerException e) {
            return 1;
        } catch(Exception e){
            return 2;
        }finally{
            return 3;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/halfgap/article/details/81346862