首頁  >  文章  >  Java  >  Java 中的 throw 關鍵字

Java 中的 throw 關鍵字

WBOY
WBOY原創
2024-08-30 15:22:07348瀏覽

Java throws 是一個用方法名稱宣告的關鍵字,異常名稱方法在呼叫時可能會引發。宣告關鍵字有助於異常處理,並讓編譯器知道特定方法拋出檢查異常,必須在編譯時宣告異常,例如 IOException 或 ClassNotFoundException。如果不使用 try-catch 區塊或 throws 關鍵字處理已檢查的異常,編譯器將引發編譯時錯誤。該關鍵字還可以幫助程式設計師透過了解該方法拋出異常來開發高效的應用程式。

文法:

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

Access_specifierreturn_typemethod_namethrowsexception_name

這裡,

  • Access_specifier:這是指告訴 JVM 從哪裡可以存取特定方法的關鍵字。例如,私有/公有或受保護。
  • Return_type:這是指告訴被呼叫方法傳回的物件的資料類型的關鍵字,例如 int、Boolean、String 等
  • Method_name:指需要呼叫的方法名稱。
  • exception_name 可以是進程在執行程式流程時可能引發的內建或自訂例外。

例如:

public void my_method() throws MyException

Java 中的 Throws 關鍵字如何運作?

錯誤和異常對於 Java 應用程式非常重要。它有助於確定是否發生了不合適的情況,例如語法錯誤或輸入的輸入與資料不符。此外,錯誤是不可避免的,只會導致編譯錯誤並不可預測地停止應用程式執行;可以在一定程度上處理異常。

Java 中的 throw 關鍵字

處理異常意味著如果異常發生,如何以正確且果斷的方式停止執行。

有兩種類型的例外:

  • 未檢查異常:這些類型的異常是運行時異常,如果在程式碼中處理它們,則編譯器不會檢查它們。範例 - 算術異常、IndexOutOfBoundsException 等。對這些異常使用 throws 關鍵字是沒有意義的。
  • 已檢查異常:這些是編譯器在編譯時檢查的異常類型,以查看它們是否正在處理。因此,如果不處理這些異常,編譯器將拋出錯誤 - 未處理的異常類型。範例 – IOException、ClassNotFoundException。

兩種處理方式

因此,有兩種方法來處理檢查異常:

1.嘗試捕捉

使用 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");
}
}
}

輸出:

Java 中的 throw 關鍵字

說明:在上面的範例中,我們聲明了一個自訂異常,並使用 throw 關鍵字在 main 方法中明確拋出它。一旦控制進入方法控制並拋出異常,它就會直接進入catch區塊,執行這些語句,然後退出程式。

2.拋出關鍵字

用方法名稱宣告此關鍵字告訴編譯器方法可以拋出例外。此關鍵字主要與 throw 關鍵字混淆,用於在我們的程式碼中或在處理自訂例外狀況時故意拋出例外狀況。

  • 只拋出關鍵字讓我們可以在發生異常時執行語句。它不能用來避免異常的發生。因此,它用於異常處理。
  • throws 關鍵字常與 throw 混淆,用於明確拋出異常。而 throws 就是用來處理的。
  • 如果出現異常,row關鍵字可以幫助程式設計師讓程式順利有效率的運作。

在 Java 中實作 Throws 關鍵字的範例

我們可以用兩種方式使用 throws 關鍵字:

範例#1

首先,我們可以在拋出異常的方法的聲明中使用 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");
}
}

輸出:

Java 中的 throw 關鍵字

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.

Example #2

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:

Java 中的 throw 關鍵字

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.

Conclusion

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn