未解决的编译:未处理的异常类型IOException [英] Unresolved compilation: Unhandled exception type IOException

查看:315
本文介绍了未解决的编译:未处理的异常类型IOException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从标准中读取int时,出现编译错误。

When trying to write read an int from standard in I'm getting a compile error.

System.out.println("Hello Calculator : \n");        
int a=System.in.read();

程序抛出异常:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type IOException at SamplePackege.MainClass.main(MainClass.java:15)

如何解决此错误?

我的代码:

try {
    Scanner sc = new Scanner(System.in);
    int a=sc.nextInt();
} catch (Exception e) {
    // TODO: handle exception
}


推荐答案

in.read()可以引发IOException类型的已检查异常。

in.read() can throw a checked exception of type IOException.

您可以阅读有关异常处理的信息。在Java中此处。

You can read about Exception Handling in Java Here.

您可以更改程序以引发IOException,也可以将读取内容放入try catch块中。

You can either change your program to throw an IOException, or you can put the read in a try catch block.

try{
   int a=System.in.read();
catch(IOException ioe){
   ioe.printStackTrace();
}

public static void main(String[] args) throws IOException {
    System.out.println("Hello Calculator : \n");
    int a=System.in.read();
}

这篇关于未解决的编译:未处理的异常类型IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆