How do I fix a compilation error for unhandled exception on call to Thread.sleep()?

134,957

Thread.sleep can throw an InterruptedException which is a checked exception. All checked exceptions must either be caught and handled or else you must declare that your method can throw it. You need to do this whether or not the exception actually will be thrown. Not declaring a checked exception that your method can throw is a compile error.

You either need to catch it:

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
    // handle the exception...        
    // For example consider calling Thread.currentThread().interrupt(); here.
}

Or declare that your method can throw an InterruptedException:

public static void main(String[]args) throws InterruptedException

Related

Share:
134,957

Related videos on Youtube

Mr.Crippled
Author by

Mr.Crippled

Some experience with html and C coding

Updated on February 27, 2020

Comments

  • Mr.Crippled
    Mr.Crippled about 4 years

    I am new to Java and kind of new to programming (I know diving straight into Java probably wasn't the greatest idea.) and I've been getting an error consistently no matter how I try to add a pause in my program. I am doing a simple counting program and want to add a one second delay between each number here is the code I have so far:

    import java.lang.*;
    
    public class Counter
    {
        public static void main(String[]args)
        {
            int i;
    
            for (i = 0; i <= 10; i++)
            {
                Thread.sleep(1000);
                System.out.println(i);
            }
            System.out.println("You can count to ten.");
        }
    }
    

    The call to Thread.sleep() won't compile. The javac compiler says, "unreported exception InterruptedException; must be caught or declared to be thrown" and Eclipse says, "Unhandled exception type InterruptedException"

    • Brian Roach
      Brian Roach over 11 years
      And what would that error be?
    • Andrew Logvinov
      Andrew Logvinov over 11 years
      Compile error since Thread.sleep() might throw InterruptedException.
    • Brian Roach
      Brian Roach over 11 years
      @AndrewLogvinov - Yes, I know that ... the point being that the question needs improving ;)
    • Alex
      Alex over 11 years
      Just a tip, Java.lang.* does not need to be imported for every Java program you write
    • Mr.Crippled
      Mr.Crippled over 11 years
      Andrew you're correct it is an InterruptedException. Thank you Alex, I was reusing another program lol. Thank you guys for responding with such alacrity.
    • Ripon Al Wasim
      Ripon Al Wasim almost 10 years
      Write the code Thread.sleep(1000); in a try block
  • Brian Roach
    Brian Roach over 11 years
    +1 - with the suggestion that for a beginner perhaps a link to the oracle tutorial on exceptions and pointing out the javadoc states which exceptions a method may throw. (Edit: you read my mind)
  • Mr.Crippled
    Mr.Crippled over 11 years
    Thank you Mark for answering but now you've left me wondering why I would need to declare an exception in the first place. Brian, you wouldn't happen to have the link to the oracle tutorial on exceptions would you?
  • sactiw
    sactiw over 8 years
    @Mr.Crippled you are missing the point here, declaration doesn't mean throwing a exception rather a exception may be thrown and thus should be catch by caller and acted accordingly (or you catch it there itself and take action by surrounding it in a try/catch block). Now, there are unchecked (runtime) exception as well which don't need to be declared, e.g. NullPointerException, is unchecked exception and doesn't needs to be declared. Why so? Well checked exception let you catch it and act on it or let the caller handle it but with unchecked (runtime) exceptions you mostly want system to fail.