Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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"

like image 408
Mr.Crippled Avatar asked Dec 22 '12 18:12

Mr.Crippled


People also ask

How do you handle an unhandled exception in the thread?

Uncaught exception handler will be used to demonstrate the use of exception with thread. It is a specific interface provided by Java to handle exception in the thread run method. There are two methods to create a thread: Extend the thread Class (java.

Why does thread sleep throw an exception?

sleep() Method: Method Whenever Thread. sleep() functions to execute, it always pauses the current thread execution. If any other thread interrupts when the thread is sleeping, then InterruptedException will be thrown.

Which exception does the thread sleep through?

Because if a Thread is sleeping, the thread may be interrupted e.g. with Thread. interrupt() by another thread in which case the sleeping thread (the sleep() method) will throw an instance of this InterruptedException . Example: Thread t = new Thread() { @Override public void run() { try { System.

Which type of exception is thrown by sleep method?

Notice that main declares that it throws InterruptedException . This is an exception that sleep throws when another thread interrupts the current thread while sleep is active.


1 Answers

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

  • Lesson - Exceptions
  • When does Java's Thread.sleep throw InterruptedException?
  • Java theory and practice: Dealing with InterruptedException
like image 103
Mark Byers Avatar answered Sep 22 '22 15:09

Mark Byers