-1

I've looked up where to put it and all I'm getting are mixed answers and the purpose of this is to create rudimentary for the alpha version of this game. Here is the code:

public class Intelijence {
    public static void main(String[] args) {
        System.out.println("OK, That looks perfect");
        Thread.sleep(5000);
        System.out.println("Huh, What's that");
    }
}

I know that there is no throws InterruptedException. I'm just wondering where to put it.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80

4 Answers4

1

You can declare it in the throws clause on the main method. If you are certain that the sleep will not be interrupted, this is the best option.

public class Intelijence {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("OK, That looks perfect");
        Thread.sleep(5000);
        System.out.println("Huh, What's that");
    }
}

You could also catch it if it requires handling.

public class Intelijence {
    public static void main(String[] args){
        System.out.println("OK, That looks perfect");
        try{
            Thread.sleep(5000);
        } catch(InterruptedException e){
            //handle exception
            e.printStackTrace();
        }
        System.out.println("Huh, What's that");
    }
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

Use something like this i.e. Adding throws keyword in the method signature itself which will pass the exception to the calling method:

public class Intelijence {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("OK, That looks perfect");
        Thread.sleep(5000);
        System.out.println("Huh, What's that");
    }
}

Or

Use something like i.e. Try-Catch blocks to handle that exception then and there only:

public class Intelijence {
        public static void main(String[] args){
            System.out.println("OK, That looks perfect");
            try {Thread.sleep(5000);}
            catch (InterruptedException e){ e.printStackTrace();}
            System.out.println("Huh, What's that");
        }
    }

I think this will help you.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Sanket Singh
  • 1,246
  • 1
  • 9
  • 26
0

Thread.sleep throws an InterruptedExcpetion - you need to either catch it, or declare a throws clause in the method the calls it - in this case, main:

public class Intelijence {
    public static void main(String[] args) throws InterruptedException {
        // Here ---------------------------^
        System.out.println("OK, That looks perfect");
        Thread.sleep(5000);
        System.out.println("Huh, What's that");
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

You have two primary options

  1. declare that main throws interrupted-exception

  2. just put try { } catch () {} around the sleep statement, and enter a short error message stating something like "Should never get here"

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • I should point out that this is really a design decision - what does the OP **want** to happen if the sleep is interrupted? (1) hand the decision over to someone else? (2) ignore the exception? The two aren't exactly equivalent. [In this case the second seems appropriate] – user13784117 Jul 04 '20 at 19:00