[英]How to create own annotation for junit that will skip test if concrete exception was thrown during execution?
[英]Skip execution if exception thrown
我坚持一些非常基本的东西。 在我们的游戏中,我们有一个leveleditor / loader,可以通过URL来获取关卡。 现在,如果URL指向不存在的文件,则编辑器应拒绝加载该级别并仅停留在当前级别,我只是在与基本代码苦苦挣扎。
private void loadLevel(URL url) {
Scanner in = null;
try {
in = new Scanner(new BufferedReader(new InputStreamReader(
url.openStream())));
readLine(in);
in.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
本质上,如果抛出FileNotFound
(或任何其他),则readLine(in)
不应继续进行。 各种NPE(如果有)。
private void loadLevel(URL url) {
Scanner in = null;
try {
in = new Scanner(new BufferedReader(new InputStreamReader(
url.openStream())));
/*if(in!=null){
readLine(in);
in.close();
}*/
readLine(in);
in.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
编辑:@LuiggiMendoza的建议之后。
finally
使用throws
。 让调用函数处理它。 我还没有测试过,但是这种想法就是...
private void loadLevel(URL url) throws IOException, FileNotFoundException {
Scanner in = null;
try {
in = new Scanner(new BufferedReader(new InputStreamReader(
url.openStream())));
if (in == null) throw new FileNotFoundException();
readLine(in);
}
finally {
in.close();
}
}
在一个线程的上下文中,如果下面的这一行引发异常,则下一行代码将不执行。 如果您认为自己的代码在执行其他操作,则可能是另一个线程在执行此操作/其他一些代码
in = new Scanner(new BufferedReader(new InputStreamReader(
url.openStream())));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.