簡體   English   中英

為多個異常創建捕獲塊

[英]Creating a Catch Block For Multiple Exceptions

我正在嘗試編寫由三個類組成的程序,每個類都有一個例外-使用繼承。 在我們的測試人員類中,我們假設使用catch塊,但是我很難理解這個想法。

該計划的成果如下:

  • 如果該句子以句點(。),感嘆號(!)或問號(?)以外的其他結尾,則程序應引發PunctuationException。
  • 如果該句子專門以逗號結尾,則程序應引發CommaException。
  • 如果捕獲到一般的PunctuationException,則程序應輸出“句子未正確結束”。
  • 如果捕獲到CommaException,則程序應顯示“您不能以逗號結束句子”。
  • 如果沒有例外,程序應打印“句子正確結束”。 然后終止

使用catch塊捕獲所有EndOfSentenceExceptions,導致程序打印一條消息並終止。

public class EndOfSentenceException extends Exception
{
    public EndOfSentenceException(String str) 
    {
        super("The sentence ends correctly.");
    }
}
public class PunctuationException extends EndOfSentenceException
{
    public PunctuationException(String str) 
    {
        super("The sentence does not end correctly.");
    }
}
public class CommaException extends PunctuationException
{
    public CommaException(String str) 
    {
        super("You can't end a sentence in a comma.");
    }
}
public class Driver
{
    public static void checkSentence(String str)
            throws EndOfSentenceException
            {
                if(!(str.endsWith(".") || str.endsWith("!") || str.endsWith("?")))
                    {
                        throw new PunctuationException(str);
                    }
                if(str.endsWith(","))
                    {
                        throw new CommaException(str);
                    }
            }
    public static void main(String[] args) 
            throws EndOfSentenceException
    {
        //Initialize a scanner to take user input
        Scanner scan = new Scanner(System.in);
        String input = "";

        //Ask the user to input their sentence and assign it to input
        System.out.println("\nEnter a sentence:");
        input = scan.nextLine();

        try 
        {
            checkSentence(input);
            System.out.println("The Sentence ends correctly.");
        }
        catch(PunctuationException error)
        {
            System.out.println(error.getMessage());
        }
        catch(CommaException error)
        {
            System.out.println(error.getMessage());
        }
        scan.close();
    }
}

java中的異常應擴展Throwable (通常是ExceptionRuntimeException )。 因此,您的自定義異常可能如下所示:

public class PunctuationException extends Exception {
    public PunctuationException() {
         super("The sentence does not end correctly");
    }

    public PunctuationException(String message) {
        super(message);
    }
}

public class CommaException extends PunctuationException {
    public CommaException() {
        super("You can't end a sentence in a comma.");
    }
}

其次 ,異常本身不應對檢查條件負責。 此檢查不應例外。

class Driver {
    public static void main(String... args) {
        try {
            checkSentence("....");
            System.out.println("The sentence ends correctly.");
        } catch(PunctuationException e) {
            System.out.println(e.getMessage());
        }
    }

    public static void checkSentence(String str) throws PunctuationException {
        str = str != null ? str.trim() : null;

        if (str == null)
            return;
        if (!str.endsWith(".") && !str.endsWith("!") && !str.endsWith("?"))
            throw new PunctuationException();
        if (str.endsWith(","))
            throw new CommaException();
    }
}

我同意oleg.cherednik。

也許,在像他提到的那樣創建自定義異常類之后,可以使測試器類如下:

public class TesterClass{
    public void checkSentenceForException() {
     try {
         checkInputForEnding(input); //this function will throw the particular user-defined exception 
         System.out.println("The sentence ends correctly.");
     }
     catch(PunctuationException e) {
         //Desired exception output
     }
     catch(CommaException e) {
         //Desired exception output
     }
     finally {
         //if any cleanup is required
     }
    }
}

如果可以做得更好,請分享。

暫無
暫無

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

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