System.exit(1)并返回

时间:2014-01-02 17:02:52

标签: java error-handling try-catch

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()终止,而catch addRecords()块终止于return 。是否有推荐的方法何时应该使用每种方法?

4 个答案:

答案 0 :(得分:14)

他们做不同的事情。 return只是从函数返回到其调用者,程序继续运行。 System.exit()终止该计划;控制不会返回给调用者。

当程序遇到不可恢复的错误时,您应该使用System.exit()并且程序中没有任何意义继续运行。当程序可以正常恢复时,或者程序在退出之前执行清理/关闭操作时,请使用return

另见more extended discussion of System.exit()

答案 1 :(得分:1)

return应该是break,只是离开while循环,以便完成scanner.close()

System.exit样式不好,但对于命令行程序是可行的。不捕获异常会有些相同:使用消息终止,但随后也会使用堆栈跟踪。 在这个让函数抛出异常将是更好的方式。

答案 2 :(得分:0)

return 语句在方法内部使用。 System.exit(0)用于任何方法退出程序。 System.exit(0)以narmally方式终止程序。由于程序中遇到一些错误, System.exit(1)终止程序。

答案 3 :(得分:-3)

System.exit()在调用行终止程序。 如果发生错误,System.exit(1)将终止程序。

相关问题