java try后没有catch,只有finnally

Posted fengyanqing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java try后没有catch,只有finnally相关的知识,希望对你有一定的参考价值。

今天看jdk1.6源码  ThreadPoolExecutor中Worker的runTask方法   

catch(RunTimeException ex) 中 tthrow ex,会把ex抛到上层,上层try没有catch异常,该异常还会往上层抛,

try后直接跟finnally,finnally中runLock.unlock(),会释放锁;

总结:try....finnally 的用法主要是为了释放资源,不进行异常捕获,将异常交由上层调用者处理;

 

        private void runTask(Runnable task) {
            final ReentrantLock runLock = this.runLock;
            runLock.lock();
            try {
                /*
                 * If pool is stopping ensure thread is interrupted;
                 * if not, ensure thread is not interrupted. This requires
                 * a double-check of state in case the interrupt was
                 * cleared concurrently with a shutdownNow -- if so,
                 * the interrupt is re-enabled.
                 */
                if ((runState >= STOP ||
                    (Thread.interrupted() && runState >= STOP)) &&
                    hasRun)
                    thread.interrupt();
                /*
                 * Track execution state to ensure that afterExecute
                 * is called only if task completed or threw
                 * exception. Otherwise, the caught runtime exception
                 * will have been thrown by afterExecute itself, in
                 * which case we don‘t want to call it again.
                 */
                boolean ran = false;
                beforeExecute(thread, task);
                try {
                    task.run();
                    ran = true;
                    afterExecute(task, null);
                    ++completedTasks;
                } catch (RuntimeException ex) {
                    if (!ran)
                        afterExecute(task, ex);
                    throw ex;
                }
            } finally {
                runLock.unlock();
            }
        }

以上是关于java try后没有catch,只有finnally的主要内容,如果未能解决你的问题,请参考以下文章

Java 异常处理

下面有关Java异常处理模型的说法错误的是

异常处理——Java的try catch用法

Java Try Catch finally 没有 Catch 的块

Java trycatch语句

java try catch 异常后还会继续执行吗