Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6017961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:12:17+00:00 2026-05-23T03:12:17+00:00

Possible Duplicate: Does a finally block always run? I learned that the finally clause

  • 0

Possible Duplicate:
Does a finally block always run?

I learned that the finally clause of a try catch statement, executes always. But some guy said to me that it is possible to avoid executing it(removing it is not an option).

-Does someone how is that possible?

-Also i am curious in knowing why would someone want to avoid to execute it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:12:18+00:00Added an answer on May 23, 2026 at 3:12 am

    Kill it with an uncaught exception within the finally block, or kill the overall JVM (which kills the thread, among other things).

    There is no good reason to stop the execution of a finally block except poor design. If it’s not supposed to run every time, then don’t put it in a finally block.


    Using the below test code, I run two different scenarios to see what happens when killing the Thread:

    1. Start the Thread and sleep the main thread for 2 seconds. Within the Thread, pretty much immediately enter the finally block and then sleep for 5 seconds. Once the main thread is finished waiting, kill the Thread using stop.
    2. Start the Thread and sleep 2 seconds. Within the Thread, sleep 5 seconds before entering the finally block and then sleep some more within the finally to give it a chance to be killed.

    In the first case, the result is that the finally block stops executing.
    In the second case, the result is that the finally block executes completely, and on the Thread that was stopped no less.

    Output (note the name of the current thread added for all output):

    thread-starting [main]
    trying [Thread-0]
    catching [Thread-0]
    finally-sleeping [Thread-0]
    thread-stopped [main]
     [main]
    thread-starting [main]
    trying-sleeping [Thread-1]
    thread-stopped [main]
    finally-sleeping [Thread-1]
    finally-done [Thread-1]
    

    Code:

    public class Main
    {
        public static void main(String[] args)
        {
            testThread(new TestRunnable());
            println("");
            testThread(new TestRunnable2());
        }
    
        private static void testThread(Runnable runnable)
        {
            Thread testFinally = new Thread(runnable);
    
            println("thread-starting");
    
            testFinally.start();
    
            try
            {
                Thread.sleep(2000);
            }
            catch (InterruptedException e)
            {
                println("main-interrupted...");
            }
    
            testFinally.stop();
    
            println("thread-stopped");
        }
    
        private static class TestRunnable implements Runnable
        {
            @Override
            public void run()
            {
                try
                {
                    println("trying");
                    throw new IllegalStateException("catching");
                }
                catch (RuntimeException e)
                {
                    println(e.getMessage());
                }
                finally
                {
                    println("finally-sleeping");
    
                    try
                    {
                        Thread.sleep(5000);
                    }
                    catch (InterruptedException e)
                    {
                        println("finally-interrupted");
                    }
    
                    println("finally-done");
                }
            }
        }
    
        private static class TestRunnable2 implements Runnable
        {
            @Override
            public void run()
            {
                try
                {
                    println("trying-sleeping");
    
                    Thread.sleep(5000);
                }
                catch (InterruptedException e)
                {
                    println("trying-interrupted");
                }
                finally
                {
                    println("finally-sleeping");
                    try
                    {
                        Thread.sleep(5000);
                    }
                    catch (InterruptedException e)
                    {
                        println("finally-interrupted");
                    }
                    println("finally-done");
                }
            }
        }
    
        private static void println(String line)
        {
            System.out.printf("%s [%s]%n", line, Thread.currentThread().getName());
            System.out.flush();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicates: Conditions when finally does not execute in a .net try..finally block In
Possible Duplicate: In Java, does return trump finally? Wondering if finally statement will still
Possible Duplicate: Will code in a Finally statement fire if I return a value
Possible Duplicate: Does COUNT(*) always return a result? Is it possible in any scenario
Possible Duplicate: What does the Visual Studio "Any CPU" target mean? I've noticed that
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: What does map(&:name) mean in Ruby? I was watching a railscast and
Possible Duplicate: Why does C# not provide the C++ style ‘friend’ keyword? I'd like
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: Does it make sense to use the <table> tag on a “modern”

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.