Java throws 是一個用方法名稱宣告的關鍵字,異常名稱方法在呼叫時可能會引發。宣告關鍵字有助於異常處理,並讓編譯器知道特定方法拋出檢查異常,必須在編譯時宣告異常,例如 IOException 或 ClassNotFoundException。如果不使用 try-catch 區塊或 throws 關鍵字處理已檢查的異常,編譯器將引發編譯時錯誤。該關鍵字還可以幫助程式設計師透過了解該方法拋出異常來開發高效的應用程式。
文法:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
Access_specifierreturn_typemethod_namethrowsexception_name
這裡,
例如:
public void my_method() throws MyException
錯誤和異常對於 Java 應用程式非常重要。它有助於確定是否發生了不合適的情況,例如語法錯誤或輸入的輸入與資料不符。此外,錯誤是不可避免的,只會導致編譯錯誤並不可預測地停止應用程式執行;可以在一定程度上處理異常。
處理異常意味著如果異常發生,如何以正確且果斷的方式停止執行。
有兩種類型的例外:
因此,有兩種方法來處理檢查異常:
使用 try-catch,將可能引發異常的語句放入 try 區塊中,如果引發異常,控制權將轉到 catch 區塊中的語句來執行它們。這樣,我們就可以在發生異常時控制應用程式的流程。
代碼:
//package Proc; class MyCustomeException extends Throwable{ MyCustomeException(String s){ super(s); } } public class prac1 { public static void main(String[] args) { try{ System.out.println("Now exception is being raised"); throw new MyCustomeException("Custom exception is thrown"); } catch(MyCustomeException e){ System.out.println("Here exception is caught and handled"); } } }
輸出:
說明:在上面的範例中,我們聲明了一個自訂異常,並使用 throw 關鍵字在 main 方法中明確拋出它。一旦控制進入方法控制並拋出異常,它就會直接進入catch區塊,執行這些語句,然後退出程式。
用方法名稱宣告此關鍵字告訴編譯器方法可以拋出例外。此關鍵字主要與 throw 關鍵字混淆,用於在我們的程式碼中或在處理自訂例外狀況時故意拋出例外狀況。
我們可以用兩種方式使用 throws 關鍵字:
首先,我們可以在拋出異常的方法的聲明中使用 throws。
代碼:
//package Proc; public class prac1 { public static void main(String[] args)throws IllegalAccessException { System.out.println("Hello Everyone lets start our learning with throws keyword"); throw new IllegalAccessException("My Exception"); } }
輸出:
Explanation: In the above example, we have used the throws keyword to handle the exception being thrown using the throw keyword. In case an exception is raised, it will not prevent the program’s abnormal termination; instead, it helps to convince the compiler. Also, it helps to inform the programmer that the method might throw an exception and needs exception handling.
Second, we use a try-catch block while calling a method that is likely to throw an exception. We have made a custom exception here named MyCustomeException that is being thrown by the method throwsDemo.
Code:
//package Proc; class MyCustomeException extends Throwable{ MyCustomeException(String s){ super(s); } } public class prac1 { static void throwsDemo() throws MyCustomeException { System.out.println("We are Inside the method"); throw new MyCustomeException("Custom exception is thrown"); } public static void main(String[] args)throws IllegalAccessException { try{ System.out.println("Now exception is being raised"); throwsDemo(); } catch(MyCustomeException e){ System.out.println("Here exception is caught and handled"); } } }
Output:
Explanation: In the above example, one method throws a new exception. This is indicated using the throws keyword. But when the method is called in the main method, we can use a try-catch block or declaring a throws keyword with the main method to handle the exception.
Throws keyword is used for exception handling in Java, where one needs to handle the flow of the program when a checked exception occurs. This is different from the throw keyword and must only be used with the checked exception since it does not prevent the occurrence of an exception but helps the way that must execute if one occurs.
以上是Java 中的 throw 關鍵字的詳細內容。更多資訊請關注PHP中文網其他相關文章!