如何在 try 块移动到 catch 之前执行所有行?
我必须读取 try 块中的文件,然后将其打印出来。当打印方法正在工作时,程序不会运行该方法。我该如何解决这个问题?我无法将其保留在 while 循环中。
Lexer.java
private boolean atEOF = false;
private SourceReader source;
public static void main(String args[]) {
Token token;
try {
Lexer lex = new Lexer(args[0]);
while(!(lex.atEOF)) {
token = lex.nextToken();
}
lex.source.printVec(); // WANT TO EXECUTE THIS METHOD
} catch (Exception e) {
System.out.println("usage: java lexer.Lexer filename.x");
System.exit(-1);
}
}
SourceReader.java
public void printVec() {
System.out.println("in the program");
for (String l : progVec) {
System.out.println(l);
}
}
如何在 Lexer.java 中的 while
循环之后运行 printVec()
?
I have to read a file in try block and later print it out. While the print method is working, the program is not running the method. How do I solve this? I can't keep it in the while loop.
Lexer.java
private boolean atEOF = false;
private SourceReader source;
public static void main(String args[]) {
Token token;
try {
Lexer lex = new Lexer(args[0]);
while(!(lex.atEOF)) {
token = lex.nextToken();
}
lex.source.printVec(); // WANT TO EXECUTE THIS METHOD
} catch (Exception e) {
System.out.println("usage: java lexer.Lexer filename.x");
System.exit(-1);
}
}
SourceReader.java
public void printVec() {
System.out.println("in the program");
for (String l : progVec) {
System.out.println(l);
}
}
How do I run printVec()
after the while
loop in Lexer.java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只需在 try-catch 块之外声明词法分析器,然后将所需的函数也放在 catch 部分的第一行。
I would simply declare the Lexer outside of the try-catch block then put the required Function at the first line of the catch part too.