1

I have a class

class Foo {
    static void bar() throws InterruptedException {
        // do something
        Thread.sleep(1000);
    }

    static void baz(int a, int b, int c) throws InterruptedException {
        // do something
        Thread.sleep(1000);
    }
} 

Then I simply run it in my main

class Main { 
    public static void main() {
        new Thread(Foo::bar).start();
        new Thread(() -> Foo.baz(1, 2, 3)).start();
        new Thread(() -> Foo.baz(1, 2, 3)).start();
    }
}

I don't care about the InterruptedException. I tried to write a try-catch block, but, obviously, the exception is not caught. Java doesn't allow me to make main() throw either.

How can I simply ignore this exception I don't care at all about? I don't want to write a try-catch block in every thread constructor.

The exception should be thrown at times, but in this specific case I don't care about it.

marmistrz
  • 5,974
  • 10
  • 42
  • 94
  • Where did you add the try-catch block? – user1766169 Dec 06 '16 at 20:32
  • A Thread is constructed with a Runnable, and a Runnable can't throw a checked exception. – Sam Dec 06 '16 at 20:34
  • 1
    No, it's not a duplicate, because it's trivial to catch it in a single thread. And it's not trivial to catch it multithreaded without code repetition or bad design. – marmistrz Dec 06 '16 at 20:45
  • The question is a little different, but the accepted answer has a block which addresses your exact situation. – Sam Dufel Dec 06 '16 at 22:30
  • @marmistrz If you are wondering how to turn off checked exceptions, you can't. There may be a compiler plugin that effectively changes the language you are using to do so though (like Project Lombok). – NESPowerGlove Dec 06 '16 at 23:38

2 Answers2

1

Just catch the exception in your method and ignore it. You never interrupt the thread, so this will be fine.

static void bar() {
    try {
    // do something
    Thread.sleep(1000);
    } catch (InterruptedException ignored) { }
}
David Ehrmann
  • 7,366
  • 2
  • 31
  • 40
  • Sometimes we want to throw InterruptedException and not ignore it. It's just this one case I want to ignore it – marmistrz Dec 06 '16 at 20:41
1

In this solution, I defined an interface Interruptible, and a method ignoreInterruption which converts an Interruptible to a Runnable:

public class Foo {

  public static void main(String... args) {
    new Thread(ignoreInterruption(Foo::bar)).start();
    new Thread(ignoreInterruption(() -> Foo.baz(1, 2, 3))).start();
  }

  static void bar() throws InterruptedException {
    // do something
    Thread.sleep(1000);
  }

  static void baz(int a, int b, int c) throws InterruptedException {
    // do something
    Thread.sleep(1000);
  }  

  interface Interruptible {
    public void run() throws InterruptedException;
  }

  static Runnable ignoreInterruption(Interruptible interruptible) {
    return () -> { 
      try {
        interruptible.run();
      }
      catch(InterruptedException ie) {
        // ignored
      }
    };
  }

}
JimN
  • 3,120
  • 22
  • 35