java版的hello world

今天写了java版的hello world,但是发现报错,代码如下:

public class HelloWorld {
    public static void main(String[] args) {
        
        Thread.sleep(1000);
        
        System.out.println("hello,world!");        
    }
}

报错信息:

Error:(4, 21) java: 未报告的异常错误java.lang.InterruptedException; 必须对其进行捕获或声明以便抛出

是说有异常,必须要对 sleep这行代码进行try/catch,或者在main方法抛出InterruptedException异常。

在idea中开发java程序,只需要 Alt + Enter 快捷键,选择 add exception to method signature 来抛出异常,或者 ctrl + alt +T 在弹出的菜单里选择 try/catch。

public class HelloWorld {
    public static void main(String[] args) {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("hello,world!");
    }
}

 

你可能感兴趣的:(Java,&,Scala)