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 3238134
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:49:08+00:00 2026-05-17T17:49:08+00:00

What is the difference between the following ways of handling InterruptedException ? What is

  • 0

What is the difference between the following ways of handling InterruptedException? What is the best way to do it?

try {
 // ...
} catch (InterruptedException e) { 
   Thread.currentThread().interrupt(); 
}

…or:

try {
 //...
} catch (InterruptedException e) {
   throw new RuntimeException(e);
}

I’d like to also know in which scenarios are these two used.

  • 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-17T17:49:08+00:00Added an answer on May 17, 2026 at 5:49 pm

    What is the difference between the following ways of handling InterruptedException? What is the best way to do it?

    You’ve probably come to ask this question because you’ve called a method that throws InterruptedException.

    First of all, you should see throws InterruptedException for what it is: A part of the method signature and a possible outcome of calling the method you’re calling. So start by embracing the fact that an InterruptedException is a perfectly valid result of the method call.

    Now, if the method you’re calling throws such exception, what should your method do? You can figure out the answer by thinking about the following:

    Does it make sense for the method you are implementing to throw an InterruptedException? Put differently, is an InterruptedException a sensible outcome when calling your method?

    • If yes, then throws InterruptedException should be part of your method signature, and you should let the exception propagate (i.e. don’t catch it at all).

      Example: Your method waits for a value from the network to finish the computation and return a result. If the blocking network call throws an InterruptedException your method can not finish computation in a normal way. You let the InterruptedException propagate.

      int computeSum(Server server) throws InterruptedException {
          // Any InterruptedException thrown below is propagated
          int a = server.getValueA();
          int b = server.getValueB();
          return a + b;
      }
      
    • If no, then you should not declare your method with throws InterruptedException and you should (must!) catch the exception. Now two things are important to keep in mind in this situation:

      1. Someone interrupted your thread. That someone is probably eager to cancel the operation, terminate the program gracefully, or whatever. You should be polite to that someone and return from your method without further ado.

      2. Even though your method can manage to produce a sensible return value in case of an InterruptedException the fact that the thread has been interrupted may still be of importance. In particular, the code that calls your method may be interested in whether an interruption occurred during execution of your method. You should therefore log the fact an interruption took place by setting the interrupted flag: Thread.currentThread().interrupt()

      Example: The user has asked to print a sum of two values. Printing “Failed to compute sum” is acceptable if the sum can’t be computed (and much better than letting the program crash with a stack trace due to an InterruptedException). In other words, it does not make sense to declare this method with throws InterruptedException.

      void printSum(Server server) {
           try {
               int sum = computeSum(server);
               System.out.println("Sum: " + sum);
           } catch (InterruptedException e) {
               Thread.currentThread().interrupt();  // set interrupt flag
               System.out.println("Failed to compute sum");
           }
      }
      

    By now it should be clear that just doing throw new RuntimeException(e) is a bad idea. It isn’t very polite to the caller. You could invent a new runtime exception but the root cause (someone wants the thread to stop execution) might get lost.

    Other examples:

    Implementing Runnable: As you may have discovered, the signature of Runnable.run does not allow for rethrowing InterruptedExceptions. Well, you signed up on implementing Runnable, which means that you signed up to deal with possible InterruptedExceptions. Either choose a different interface, such as Callable, or follow the second approach above.

     

    Calling Thread.sleep: You’re attempting to read a file and the spec says you should try 10 times with 1 second in between. You call Thread.sleep(1000). So, you need to deal with InterruptedException. For a method such as tryToReadFile it makes perfect sense to say, “If I’m interrupted, I can’t complete my action of trying to read the file”. In other words, it makes perfect sense for the method to throw InterruptedExceptions.

    String tryToReadFile(File f) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            if (f.exists())
                return readFile(f);
            Thread.sleep(1000);
        }
        return null;
    }
    

    This post has been rewritten as an article here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any difference between following two ways of creating an object. Student s1
Will there be a difference between the following two ways of calling a function
What is the main difference between these following two ways to give a method
In VB.Net is there any difference between the following three ways of initialising object
What is the difference between the following coding ways: if (String::IsNullOrEmpty(TextBox1->Text)) and if (TextBox1->Text
What's the difference between the following ways of attaching an event handler in JQuery?
I have a question related to the following link: What's the difference between Thread
Are there any difference between following ways to init self? First: - (id)init {
What is the difference between following different ways of jQuery selections. To me they
What is the difference between following 2 ways of subscribing for an event? receiver.ConfigChanged

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.