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

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"

This question is related to java exception-handling checked-exceptions

The answer is


You can get rid of the first line. You don't need import java.lang.*;

Just change your 5th line to:

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