簡體   English   中英

System.exit(1)並返回

[英]System.exit(1) and return

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終止。 是否有推薦的方式何時應該使用?

他們做不同的事情。 return只是從函數返回給它的調用者,程序繼續運行。 System.exit()終止程序; 控件不會返回給調用者。

當程序遇到不可恢復的錯誤並且程序中沒有任何點繼續運行時,您應該使用System.exit() 當程序可以正常恢復時,或者程序在退出之前執行清理/關閉操作時,請使用return

另請參閱System.exit()更多擴展討論

return應該是break ,只是離開while循環,以便完成scanner.close()

System.exit是錯誤的樣式,但對於命令行程序是可行的。 不捕獲異常會有些相同:使用消息終止,但隨后也會使用堆棧跟蹤。 在這個讓函數拋出異常將是更好的方式。

return語句在方法內部使用。 System.exit(0)用於任何方法以退出程序。 System.exit(0)以narmally方式終止程序。由於程序中遇到一些錯誤, System.exit(1)終止程序。

System.exit()在調用行終止程序。 如果發生錯誤, System.exit(1)終止程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM