Java:如何在抛出异常后继续读取文件 [英] Java: How to continue reading a file after an exception is thrown

查看:612
本文介绍了Java:如何在抛出异常后继续读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的教授给我们分配了一个项目,我们必须从文本文件中获取命令并使用它们来驱动我们程序的流程。这些命令,如起飞,陆地,装载货物,卸载货物等,都是为了模拟类似飞机的物体。

so my professor has assigned us a project where we have to take in commands from a text file and use them to drive the flow of our program. These commands such as- Takeoff, land, load cargo, unload cargo, etc.- are meant to simulate an airplane-like object.

有时这些命令无法生成执行的意义,就像飞机在飞行中装载货物一样。因此,为了防止类似的事情发生,我们必须在我们自己的异常类中进行编码,即如果飞机在飞行中被命令装载货物,则抛出InvalidActionException

Sometimes these commands don't make sense to execute, like loading cargo while a plane is in-flight. Therefore, to prevent something like that from happening we had to code in our own exception classes i.e. "If the plane is ordered to load cargo while in flight, throw InvalidActionException"

我的问题是:如何在抛出异常后继续从文本文件中读入命令(看看它们抛出后如何,程序无法进一步发展

My question is: how can I continue to read-in commands from the text file after exceptions have been thrown (seeing as how once they are thrown, the program cannot progress further

以下是我想要做的概述:

Here's an outline of what I want to do:

    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
        }
    }

I我会很感激页。谢谢。

I would appreciate any help. Thanks.

推荐答案

Catch 相应的异常和流量将自动达到 while 循环条件。

Catch appropriate exception and flow will automatically reach while loop condition.

在导致异常后,没有额外的事情可以继续执行程序。

After cathing exception nothing extra to be done to continue the program.

这篇关于Java:如何在抛出异常后继续读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆