未解决的编译:未处理的异常类型 IOException
当尝试从标准中写入读取 int 时,我收到编译错误。
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
}
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();
The program throws an exception:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type IOException at SamplePackege.MainClass.main(MainClass.java:15)
How do I fix this error?
My Code :
try {
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
} catch (Exception e) {
// TODO: handle exception
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
in.read() 可以抛出 IOException 类型的已检查异常。
您可以在 此处阅读有关 Java 中的异常处理的信息。
您可以更改您的程序以引发 IOException,或者您可以将读取放入 try catch 块中。
或者
in.read() can throw a checked exception of type IOException.
You can read about Exception Handling in Java Here.
You can either change your program to throw an IOException, or you can put the read in a try catch block.
or
该程序没有错误。
read()
方法要求您捕获Exception
,以防出现问题。将方法包含在
try/catch
语句中:在任何情况下,我强烈建议您使用文档和/或 Java 教程,其中清楚地说明了这些内容。不使用它们的编程是毫无意义的。你将为自己省去很多麻烦,也可能节省我们的时间。
The program doesn't have a bug.
The method
read()
requires you to catch anException
in case something goes wrong.Enclose the method inside a
try/catch
statement:In any case I strongly suggest you to use documentation and/or Java tutorials, in which these things are clearly stated. Programming with out using them is just pointless. You will save yourself a lot of headaches, and probably also our time.