Java:未捕获的异常错误?
我将此代码提交给 KATTIS 在线代码测试器。它应该返回重复给出的两个数字的差值,直到达到 EOF。它在 Eclipse 中工作得很好,但 KATTIS 说“没有捕获异常”。我希望得到一些帮助来了解哪些异常尚未被捕获。
提供了导入和类“Kattio”,因此输入和输出始终与在线代码系统一起使用。
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStream;
class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int getInt() {
return Integer.parseInt(nextToken());
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
public class Hello {
public static void main(String[] args) {
Kattio io = new Kattio(System.in, System.out);
while (io.hasMoreTokens()){
int n1 = io.getInt();
int n2 = io.getInt();
if (n1>n2){
io.println(n1-n2);
}
else if (n2>n1){
io.println(n2-n1);
}
else {
io.println("0");
}
}
io.close();
}
}
I submitted this code to the KATTIS online code tester. It's supposed to return the difference of two numbers that have been given repeatedly until it reaches an EOF. It works just fine in eclipse, but KATTIS says that "An exception was not caught". I was hoping for some help as to what exception has not been caught.
The imports and class "Kattio" were provided so the input and output would always work with the online code system.
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStream;
class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int getInt() {
return Integer.parseInt(nextToken());
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
public class Hello {
public static void main(String[] args) {
Kattio io = new Kattio(System.in, System.out);
while (io.hasMoreTokens()){
int n1 = io.getInt();
int n2 = io.getInt();
if (n1>n2){
io.println(n1-n2);
}
else if (n2>n1){
io.println(n2-n1);
}
else {
io.println("0");
}
}
io.close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是一个猜测。
使用不同类型的数据查找代码中的异常。如果一行包含非整数,您的程序将终止。它可能应该寻找下一个令牌?
just a guess.
Look for exception in your code with different types of data. If a line with non integer your program will terminate. It should probably look for next token?
请通过添加 try/catch 来修改您的 main(),如下所示:
通常您需要更多本地化异常处理,但这至少允许您复制粘贴我们堆栈跟踪,以便我们可以看到它。
Please modify your main() by adding try/catch like so:
Normally you would want more localized exception handling but this will at least allow you to copy-paste us the stacktrace so we can see it.
我不熟悉 Kattis 在线代码测试器,但假设它检查可能抛出的未检查异常(未捕获的已检查异常将导致代码无法编译)。我在代码中看不到检查下一个标记是否为整数的任何地方,因此如果 Integer.parseInt 尝试解析不是整数的内容,它将抛出 NumberFormatException。
根据应用程序的受众,您可以保持原样(如果受众是能够理解该异常的 Java 开发人员),或者捕获它并重新抛出更用户友好的内容(如果不是)。
据推测,代码测试人员仍然会抱怨,因为它抛出了另一个(更用户友好的)异常:-)
I am unfamiliar with the Kattis online code tester but assume it checks for unchecked exceptions that may be thrown (an uncaught checked exception would cause the code not to compile). I can't see anywhere in the code that checks that the next token is an Integer, so if Integer.parseInt tries to parse something which isn't an integer, it will throw a NumberFormatException.
Depending on the audience for your application, you could leave this as it is (if the audience is java developers who will understand that exception) or catch it rethrow something more user-friendly (if they are not).
Presumably though, the code tester would still complain though as it's throwing another (more user-friendly) exception :-)