簡體   English   中英

未處理的異常類型 Java InstantiationException

[英]Unhandled exception type Java InstantiationException

我正在嘗試向我的代碼添加一個異常,以便在 securityManager 返回 false 時拋出異常:

public Appointment(String date,String time) {
    //Secure Object Construction
    if (securityManager(date, time)) {
        this.date=date;
        this.time=time;
        System.out.println("Valid");
    }
    //Invalid Object, Throw Error
    else {
        throw new InstantiationException("Date provided is Invalid");
    }
    return;

但是,當我添加它時,我有一個語法錯誤:

Unhandled exception type InstantiationException

IDE 想要添加以下行:

public Appointment(String date,String time) throws InstantiationException

但是,當我這樣做時,總是會觸發異常。

如何重新格式化代碼,以便僅在 securityManager 返回 false 時才會發生異常?

InstantiationException是一個檢查異常。 你必須處理它,否則編譯器會抱怨這個異常。

已檢查異常的示例有ClassNotFoundExceptionIOExceptionSQLException等。

如果你想在運行時引發異常,你可以使用未經檢查的異常,它們是ErrorRuntimeException子類型。 IDE 永遠不會要求處理這些異常。

暫無
暫無

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

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