美文网首页
Java_exception

Java_exception

作者: 华山野老 | 来源:发表于2013-09-10 10:39 被阅读0次

    RuntimeException#

    • You never need to write an exception specification saying that a method might throw a RuntimeException,
      because they are unchecked exceptions. it's dealt with automatically.

    • Keep in mind that only exceptions of type RuntimeException can be ignored in your coding

    • finally return之前仍然会执行


    Pitfall#

    • It's possible for an exception to simply be lost.

    This happens with a particular configuration using a finally clause

        public static void main(String[] args) {
        try {
            LostMessage lm = new LostMessage();
            try {
            lm.f();
            } finally {
            lm.dispose();  //throw another exception will be         
                              replace the former exception
            }
            } catch(Exception e) {
            System.out.println(e);
            }
        }
        
    
    • An even simpler way to lose an exception is just to return from inside a finally clause:
        public static void main(String[] args) {
        try {
                throw new RuntimeException();
            } finally {
            // Using ‘return’ inside the finally block
            // will silence any thrown exception.
            return;
            }
        }
        
    

    相关文章

      网友评论

          本文标题:Java_exception

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