特定问题的例外情况

时间:2016-03-27 02:24:04

标签: java exception project

我正在编写一个代码,如果文件不存在或文件上有无效指令,则会导致异常。如何获取代码以在我的代码中引发无效指令异常

2 个答案:

答案 0 :(得分:0)

你需要捕获FileNotFoundException,然后重新抛出你想要的任何异常。见下文..

catch(FileNotFoundException e){
    throw new InvalidRobotInstructionException(e);
}

答案 1 :(得分:0)

不要抓住InvalidRobotInstructionException。在while循环中,您需要检测String s = in.nextLine();是否是无效指令,如果是,那么您可以这样做:

throw new InvalidRobotInstructionException()

相关问题