java 异常(二)代码

来源:互联网 发布:已恢复的网络购彩 编辑:程序博客网 时间:2024/06/03 21:44
package com.apricotforest.cloudfollowup.patient.controller;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class TestException {    public TestException() {    }    boolean testEx() throws Exception {        boolean ret = true;        try {            ret = testEx1();            if (!ret) {                return false;            }        } catch (Exception e) {            System.out.println("testEx, catch exception");            ret = false;            throw e;        } finally {            System.out.println("testEx, finally; return value=" + ret);            return ret;        }    }    boolean testEx1() throws Exception {        boolean ret = true;        try {            ret = testEx2();            if (!ret) {                return false;            }            System.out.println("testEx1, at the end of try");            return ret;        } catch (Exception e) {            System.out.println("testEx1, catch exception");            ret = false;            throw e;        } finally {            System.out.println("testEx1, finally; return value=" + ret);            return ret;        }    }    boolean testEx2() throws Exception {        boolean ret = true;        try {            int b = 12;            int c;            for (int i = 2; i >= -2; i--) {                c = b / i;                System.out.println("i=" + i);            }            return true;        } catch (Exception e) {            System.out.println("testEx2, catch exception");            ret = false;            throw e;        } finally {            System.out.println("testEx2, finally; return value=" + ret);            return ret;        }    }    public String openFile() {        try {            FileInputStream inputStream = new FileInputStream("/Users/baowenwei/Downloads/sql.txt");            int ch = inputStream.read();            inputStream.close();            System.out.println("aaa");            return "step1";        } catch (FileNotFoundException e) {            System.out.println("file not found");            return "step2";        }catch (IOException e) {            System.out.println("io exception");            return "step3";        }finally{            System.out.println("finally block");            return "finally";        }    }    public static void main(String[] args) {        TestException testException1 = new TestException();        try {            testException1.testEx();            System.out.println(testException1.openFile());        } catch (Exception e) {            e.printStackTrace();        }    }}

 

查看原文:http://www.baowenwei.com/archives/836

0 0
原创粉丝点击