import java.io.*;
public class prelab7 {
public static void readstring(String a){
String s = "EECS 132";
StringReader s1 = new StringReader(s);
for (int n = 0; n < s.length(); n = n + 1){
char c = (char)s1. read();
System.out.print(" " + c);
}
}
}
发现 1 个错误: 文件:/Users/clara/Desktop/EECS132/prelab7.java [行:8] 错误:/Users/clara/Desktop/EECS132/prelab7.java:8:未报告的异常 java.io.IOException;必须被捕获或宣布被抛出
最佳答案
StringReader
是一个Reader
。以及 Reader
的 .read()
方法 can throw an IOException
.
您需要将方法声明更改为:
public static void readstring(String a)
throws IOException
{
// code here
}
(编辑:或使用 try/catch,如评论中所述;但是,如果您这样做,请避免使用经典的 .printStackTrace()
)
关于java - 为什么我收到错误 "must be caught or declared to be thrown"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22029790/