<legend id='1sKHP'><style id='1sKHP'><dir id='1sKHP'><q id='1sKHP'></q></dir></style></legend>

    1. <i id='1sKHP'><tr id='1sKHP'><dt id='1sKHP'><q id='1sKHP'><span id='1sKHP'><b id='1sKHP'><form id='1sKHP'><ins id='1sKHP'></ins><ul id='1sKHP'></ul><sub id='1sKHP'></sub></form><legend id='1sKHP'></legend><bdo id='1sKHP'><pre id='1sKHP'><center id='1sKHP'></center></pre></bdo></b><th id='1sKHP'></th></span></q></dt></tr></i><div id='1sKHP'><tfoot id='1sKHP'></tfoot><dl id='1sKHP'><fieldset id='1sKHP'></fieldset></dl></div>
      <tfoot id='1sKHP'></tfoot>

      <small id='1sKHP'></small><noframes id='1sKHP'>

      • <bdo id='1sKHP'></bdo><ul id='1sKHP'></ul>

      在什么情况下 finally {} 块不会执行?

      What are the circumstances under which a finally {} block will NOT execute?(在什么情况下 finally {} 块不会执行?)
      <i id='BEsju'><tr id='BEsju'><dt id='BEsju'><q id='BEsju'><span id='BEsju'><b id='BEsju'><form id='BEsju'><ins id='BEsju'></ins><ul id='BEsju'></ul><sub id='BEsju'></sub></form><legend id='BEsju'></legend><bdo id='BEsju'><pre id='BEsju'><center id='BEsju'></center></pre></bdo></b><th id='BEsju'></th></span></q></dt></tr></i><div id='BEsju'><tfoot id='BEsju'></tfoot><dl id='BEsju'><fieldset id='BEsju'></fieldset></dl></div>
          <bdo id='BEsju'></bdo><ul id='BEsju'></ul>

              <tbody id='BEsju'></tbody>

            <tfoot id='BEsju'></tfoot>
            1. <legend id='BEsju'><style id='BEsju'><dir id='BEsju'><q id='BEsju'></q></dir></style></legend>
              • <small id='BEsju'></small><noframes id='BEsju'>

              • 本文介绍了在什么情况下 finally {} 块不会执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 Java try{} ... catch{} ... finally{} 块中,finally{} 中的代码通常被认为是保证"无论 try/catch 中发生什么,都运行.但是,我知道它至少不会执行的两种情况:

                In a Java try{} ... catch{} ... finally{} block, code within the finally{} is generally considered "guaranteed" to run regardless of what occurs in the try/catch. However, I know of at least two circumstances under which it will not execute:

                • 如果 System.exit(0) 被调用;或者,
                • 如果异常一直抛出到 JVM 并且发生默认行为(即,printStackTrace() 并退出)
                • If System.exit(0) is called; or,
                • if an Exception is thrown all the way up to the JVM and the default behavior occurs (i.e., printStackTrace() and exit)

                是否有任何其他程序行为会阻止 finally{} 块中的代码执行?代码会在什么特定条件下执行?

                Are there any other program behaviors that will prevent the code in a finally{} block from executing? Under what specific conditions will the code execute or not?

                正如 NullUserException 所指出的,第二种情况实际上是不正确的.我认为这是因为标准错误中的文本在标准输出之后打印,防止在不向上滚动的情况下看到文本.:) 抱歉.

                As NullUserException pointed out, the second case is actually not true. I thought it was because the text in standard error printed after that in standard out, preventing the text from being seen without scrolling up. :) Apologies.

                推荐答案

                如果调用System.exit() 程序立即退出而不调用 finally.

                JVM 崩溃,例如Segmentation Fault,也会阻止 finally 被调用.即 JVM 在此时立即停止并生成崩溃报告.

                A JVM Crash e.g. Segmentation Fault, will also prevent finally being called. i.e. the JVM stops immediately at this point and produces a crash report.

                无限循环也会阻止 finally 被调用.

                An infinite loop would also prevent a finally being called.

                finally 块总是在抛出 Throwable 时调用.即使您调用 Thread.stop() 触发 ThreadDeath 在目标线程中被抛出.这可以被捕获(这是一个 Error) 并且 finally 块将被调用.

                The finally block is always called when a Throwable is thrown. Even if you call Thread.stop() which triggers a ThreadDeath to be thrown in the target thread. This can be caught (it's an Error) and the finally block will be called.

                public static void main(String[] args) {
                    testOutOfMemoryError();
                    testThreadInterrupted();
                    testThreadStop();
                    testStackOverflow();
                }
                
                private static void testThreadStop() {
                    try {
                        try {
                            final Thread thread = Thread.currentThread();
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    thread.stop();
                                }
                            }).start();
                            while(true)
                                Thread.sleep(1000);
                        } finally {
                            System.out.print("finally called after ");
                        }
                    } catch (Throwable t) {
                        System.out.println(t);
                    }
                }
                
                private static void testThreadInterrupted() {
                    try {
                        try {
                            final Thread thread = Thread.currentThread();
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    thread.interrupt();
                                }
                            }).start();
                            while(true)
                                Thread.sleep(1000);
                        } finally {
                            System.out.print("finally called after ");
                        }
                    } catch (Throwable t) {
                        System.out.println(t);
                    }
                }
                
                private static void testOutOfMemoryError() {
                    try {
                        try {
                            List<byte[]> bytes = new ArrayList<byte[]>();
                            while(true)
                                bytes.add(new byte[8*1024*1024]);
                        } finally {
                            System.out.print("finally called after ");
                        }
                    } catch (Throwable t) {
                        System.out.println(t);
                    }
                }
                
                private static void testStackOverflow() {
                    try {
                        try {
                            testStackOverflow0();
                        } finally {
                            System.out.print("finally called after ");
                        }
                    } catch (Throwable t) {
                        System.out.println(t);
                    }
                }
                
                private static void testStackOverflow0() {
                    testStackOverflow0();
                }
                

                打印

                finally called after java.lang.OutOfMemoryError: Java heap space
                finally called after java.lang.InterruptedException: sleep interrupted
                finally called after java.lang.ThreadDeath
                finally called after java.lang.StackOverflowError
                

                注意:在每种情况下,线程都会继续运行,即使在 SO、OOME、Interrupted 和 Thread.stop() 之后也是如此!

                Note: in each case the thread kept running, even after SO, OOME, Interrupted and Thread.stop()!

                这篇关于在什么情况下 finally {} 块不会执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Bytecode features not available in the Java language(Java 语言中不可用的字节码功能)
                ClassCastException because of classloaders?(ClassCastException 因为类加载器?)
                How can I add a Javaagent to a JVM without stopping the JVM?(如何在不停止 JVM 的情况下将 Javaagent 添加到 JVM?)
                Cannot load 64-bit SWT libraries on 32-bit JVM ( replacing SWT file )(无法在 32 位 JVM 上加载 64 位 SWT 库(替换 SWT 文件))
                Encourage the JVM to GC rather than grow the heap?(鼓励 JVM 进行 GC 而不是增加堆?)
                Why a sawtooth shaped graph?(为什么是锯齿形图形?)

                <small id='FRtcu'></small><noframes id='FRtcu'>

                  <bdo id='FRtcu'></bdo><ul id='FRtcu'></ul>

                    <tbody id='FRtcu'></tbody>

                    <legend id='FRtcu'><style id='FRtcu'><dir id='FRtcu'><q id='FRtcu'></q></dir></style></legend>

                        <tfoot id='FRtcu'></tfoot>

                        • <i id='FRtcu'><tr id='FRtcu'><dt id='FRtcu'><q id='FRtcu'><span id='FRtcu'><b id='FRtcu'><form id='FRtcu'><ins id='FRtcu'></ins><ul id='FRtcu'></ul><sub id='FRtcu'></sub></form><legend id='FRtcu'></legend><bdo id='FRtcu'><pre id='FRtcu'><center id='FRtcu'></center></pre></bdo></b><th id='FRtcu'></th></span></q></dt></tr></i><div id='FRtcu'><tfoot id='FRtcu'></tfoot><dl id='FRtcu'><fieldset id='FRtcu'></fieldset></dl></div>