首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.exit(1)和返回

System.exit(1)和返回
EN

Stack Overflow用户
提问于 2014-01-02 17:02:51
回答 4查看 18.5K关注 0票数 11
代码语言:javascript
复制
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class CreateTextFile
{
    private Formatter formatter;

    public void openFile()
    {
        try
        {
            formatter = new Formatter("clients.txt");
        }

        catch (SecurityException securityException)
        {
            System.err.println("You do not have permission to access this file");
            System.exit(1);
        }

        catch (FileNotFoundException fileNotFoundException)
        {
            System.err.println("Error opening or creating the file");
            System.exit(1);
        }
    }

    public void addRecords()
    {
        AccountRecord accountRecord = new AccountRecord();
        Scanner scanner = new Scanner(System.in);

        System.out.printf("%s%n%s%n%s%n%s%n", "To terminate input, type the end-of-file indicator", "when you are prompted to enter input", "On Unix/Linux/Mac OS X type <control> d then press Enter", "On Windows type <ctrl> z then press Enter");

        while (scanner.hasNext())
        {
            try
            {
                accountRecord.setAccountNumber(scanner.nextInt());
                accountRecord.setFirstName(scanner.next());
                accountRecord.setLastName(scanner.next());
                accountRecord.setBalance(scanner.nextDouble());

                if (accountRecord.getAccountNumber() > 0)
                    formatter.format("%d %s %s %,.2f%n", accountRecord.getAccountNumber(), accountRecord.getFirstName(), accountRecord.getLastName(), accountRecord.getBalance());
                else
                    System.out.println("Account number must be greater than 0");
            }

            catch (FormatterClosedException formatterClosedException)
            {
                System.err.println("Error writing to file");
                return;
            }

            catch (NoSuchElementException noSuchElementException)
            {
                System.err.println("Invalid input. Try again");
                scanner.nextLine();
            }

            System.out.printf("%s %s%n%s", "Enter account number (>0),", "first name, last name and balance.", "?");
        }
        scanner.close();
    }

    public void closeFile()
    {
        if (formatter != null)
            formatter.close();
    }
}

我只是想知道为什么在openFile()中,catch块用System.exit()终止,而addRecords()中的catch块用return终止。是否有一种推荐的方法来说明应该在什么时候使用?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-01-02 17:06:11

他们做的事情不一样。return只是从函数返回给它的调用者,程序继续运行。System.exit()终止程序;控件不返回调用方。

当您的程序遇到无法恢复的错误并且程序没有继续运行的时候,您应该使用System.exit()。当您的程序能够优雅地恢复时,或者当程序在退出之前执行清理/关闭操作时,请使用return

也请参阅这个System.exit()

票数 14
EN

Stack Overflow用户

发布于 2014-01-02 17:10:04

return应该是一个break,只留下while循环,这样scanner.close()就完成了。

System.exit风格不好,但对于命令行程序来说是可行的。不捕获异常在某种程度上是一样的:使用消息终止,但也使用堆栈跟踪。在这种情况下,让函数抛出异常是更好的方法。

票数 1
EN

Stack Overflow用户

发布于 2015-12-11 17:10:39

it.System.exit(0)的方法中使用返回语句,在任何方法中都要从程序中提取。System.exit(0)终止程序narmally.Whereas System.exit(1)由于程序中遇到一些错误而终止程序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20887829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档