因此,我的教授为我们分配了一个项目,我们必须从该文件中接收命令并使用它们来驱动程序的流程.这些命令-起飞,着陆,装载货物,卸载货物等-用于模拟飞机状物体.
有时执行这些命令没有意义,例如在飞机飞行中装载货物.因此,为防止此类情况发生,我们必须在自己的异常类中进行编码,即“如果飞机被命令在飞行中装载货物,则抛出InvalidActionException”
我的问题是:引发异常后,如何继续从文本文件中读取命令(看成一旦引发异常,程序将无法继续执行
这是我要做什么的概述:
Scanner input = new Scanner(new FileInputStream("stuff1.txt"));
while(input.hasNextLine())
{
try
{
//execute commands by reading them using input.nextLine()
}
catch()
{
//catch any exceptions and ignore them... continue to read
//the file for more commands
}
}
我将不胜感激任何帮助.谢谢.
解决方法:
捕获适当的异常,流将自动达到while循环条件.
排除异常后,无需执行其他任何操作即可继续执行该程序.
标签:java,io
来源: https://codeday.me/bug/20191011/1889452.html