Java 修复错误:未报告的异常 InterruptedException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18025337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:51:50  来源:igfitidea点击:

Fixing Error: Unreported Exception InterruptedException

javaexception

提问by Jonathan Lam

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:

我是 Java 新手。我只是在搜索如何让Java程序等待,它说使用该Thread.sleep()方法。但是,当我这样做时,会出现错误:

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

错误:未报告的异常 InterruptedException;必须被捕获或声明被抛出

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

我通过添加throws InterruptedException到方法声明来解决这个问题,现在它可以工作了。

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?

但是,在调用该方法时,我再次收到错误消息。人们说要使用 throw 和 catch 块,但我还不知道该怎么做。有人可以帮我吗?

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

无论如何,Draw.java 的代码(使用 sleep() 方法):

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()):

在 Square.java 中(调用 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.

谢谢。

采纳答案by Developer Marius ?il?nas

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.

提供的示例演示了如何在调用链上(方法调用链上)进行异常传递。为此,您的方法声明包含一个 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.

添加try {} catch() {}块后,从方法 DS 中删除“throws InterruptedException”

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

您可以try {} catch() {}根据需要用块包裹其他行。阅读有关Java 异常的信息

回答by JerryIndia

Simple thread program which illustrates the sleep function comes with JavaThread

JavaThread自带的简单线程程序,说明睡眠功能

class sub implements Runnable{
 public void run()
 {
     System.out.println("thread are started running...");
     try
     {
           Thread.sleep(1000);
      }
      catch(Exception e)
      {
          System.out.println(e);
       }
      System.out.println("running properly.....");
 }
 public static void main(String[] args) 
{
    sub r1=new sub();
    Thread t1=new Thread(r1);

    // with the help of start
    t1.start();  } }