Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing Error: Unreported Exception InterruptedException

Tags:

java

exception

I'm new to Java. I was just searching how to make a Java program wait, and it said to use the Thread.sleep() method. However, when I do this it comes up with an error:

error: unreported exception InterruptedException; must be caught or declared to be thrown

I fixed that by adding throws InterruptedException to the method declaration, and now it works.

However, when calling the method, I got the error again. People say to use a throw and catch block, but I'm not sure how to do that yet. Could somebody help me here?

Anyways, code for Draw.java (with sleep() method):

package graphics.utilities;

public class Draw {
  public static void DS(int[] c)
    throws InterruptedException {
    \\ .. Drawing Algorithms
    Thread.sleep(2000);
    \\ .. More Drawing Algorithms
  }
}

And in Square.java (calling DS()):

package graphics.shapes;

import graphics.utilities.*;

public class Square implements Graphics {
  int x1,y1,s;
  public Square(int x1,int y1,int s) {
    this.x1 = x1;
    this.y1 = y1;
    this.s = s;
  }
  public void GC() {
    System.out.printf("Square Coordinates:%n Start Point:%n  x: %d%n  y: %d%n Height/Width: %d%n%n" , this.x1,this.y1,this.s);
  }
  public void D() {
    int x2 = x1 + s;
    int y2 = y1;
    int x3 = x1 + s;
    int y3 = y1 + s;
    int x4 = x1;
    int y4 = y1 + s;

    int[] c = {x1,y1,x2,y2,x3,y3,x4,y4};
    Draw.DS(c);
  }
}

Thanks.

like image 534
Jonathan Lam Avatar asked Aug 02 '13 20:08

Jonathan Lam


People also ask

What is an InterruptedException in Java?

An InterruptedException is thrown when a thread is interrupted while it's waiting, sleeping, or otherwise occupied. In other words, some code has called the interrupt () method on our thread. It's a checked exception, and many blocking operations in Java can throw it. 3.1.

How do I get rid of InterruptedException in a method?

For this your method declaration contains a throws InterruptedException. After you added try {} catch () {} block, remove "throws InterruptedException" from the method DS.

How do you handle InterruptedException on call stack?

We can allow the InterruptedException to propagate up the call stack, for example, by adding a throws clause to each method in turn and letting the caller determine how to handle the interrupt. This can involve our not catching the exception or catching and rethrowing it.

Why can’t we just throw a runtime exception?

We have to inform the higher level that we just caught an interruption request. We can’t just throw a runtime exception. Such behavior would be too irresponsible. The entire thread received an interruption request, and we merely swallow it and convert it into a RuntimeException.


1 Answers

Provided example demonstrates how to do exception pass up the call chain (up the method call chain). For this your method declaration contains a throws InterruptedException.

Alternative approach is to handle exception in the method it occurred: in your case add

try 
{
    Thread.sleep(2000);
} 
catch(InterruptedException e)
{
     // this part is executed when an exception (in this example InterruptedException) occurs
}

After you added try {} catch() {} block, remove "throws InterruptedException" from the method DS.

You can wrap other lines with try {} catch() {} block as required. Read about Java exceptions.

like image 120
Developer Marius Žilėnas Avatar answered Sep 28 '22 03:09

Developer Marius Žilėnas