美文网首页
Java基础(六) | 熟悉异常处理机制

Java基础(六) | 熟悉异常处理机制

作者: 采风JS | 来源:发表于2017-07-21 17:31 被阅读0次

    Java异常处理机制,带你走进程序员的完美世界。

    万物皆可抛.png
    • Error类
      程序无法处理的错误,其超出了应用程序的控制和处理能力,表示运行应用程序中严重的问题,多数与代码编写者无关,为JVM出现的问题,常见的有OutOfMemoryError,异常发生时,JVM一般选择线程终止;

    • Exception类
      应用程序中可能的可预测和可恢复的问题,一般发生在特定方法和特定操作中,重要子类为RuntimeException;

    • 运行时异常和非运行时异常
      运行时异常,是RuntimeException类及其子类,常见的有NullPointerException、ArrithmeticException和ClassNotFoundException,原则上不进行捕获和抛出,其本质上为程序逻辑问题,期待程序员检查后恢复;
      非运行时异常,是RuntimeException以外的异常,发生在编译时期,需要进行try...catch...finally或者throws抛出;

    • 检查异常和未检查异常
      检查异常(即非运行时异常):由程序不能控制的无效外界环境决定(数据库问题,网络异常,文件丢失等),与非运行时异常处理相同;
      未检查异常(Error和运行时异常):程序自身的瑕疵和逻辑错误,在运行时发生,与运行时异常处理相同;

    • try...catch...finally使用原则
      1)try代码块中发生异常,生成异常对象,返回对象引用,程序代码终止;其后可以接零个或者多个catch代码块,没有catch代码块,必须接一个finally代码块;
      2)多个catch代码块,通常只能命中一个;
      3)finally代码块常用于关闭资源,且不处理返回值;finally块中发生异常,前面代码使用System.exit()退出程序,程序所在线程死亡,关闭CPU,此四种情况,不执行finally代码块;

    • throw和throws区别
      throws为方法抛出异常,由方法调用者处理,与方法调用者处理方式无关;
      throw为语句抛出异常,不可单独使用,与try...catch和throws配套使用;

    • return语句与Java异常处理机制
      finally中包含return语句,等同于告诉编译器该方法无异常,即使存在异常,调用者也会捕获不到异常,值得注意!
      Java中处理异常中return关键字

    public class TestException {  
        public TestException() {  
        }  
      
        boolean testEx() throws Exception {  
            boolean ret = true;  
            try {  
                ret = testEx1();  
            } 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 static void main(String[] args) {  
            TestException testException1 = new TestException();  
            try {  
                testException1.testEx();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    }
    
    // 打印结果
    i=2
    i=1
    testEx2, catch exception
    testEx2, finally; return value=false
    testEx1, finally; return value=false
    testEx, finally; return value=false
    

    相关文章

      网友评论

          本文标题:Java基础(六) | 熟悉异常处理机制

          本文链接:https://www.haomeiwen.com/subject/pgpekxtx.html