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

Java 中的 throw 關鍵字

WBOY
WBOY原創
2024-08-30 15:21:54242瀏覽

這個 Throw 關鍵字主要用於異常處理的概念。顧名思義, throw 關鍵字的作用是從程式中向編譯器拋出異常。異常簡報是編譯器在程式碼運行期間出現任何差異時從程式中彈出的一種錯誤。使用這個 throw 關鍵字,我們可以透過解釋程式執行時間錯誤的任何原因來定義異常。

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

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

讓我們看到下面更多細節:

文法

Java 中的 throw 語法如下:

throw exception_type ;

throw instance;

Throw 關鍵字在 Java 中如何運作?

在這裡,我們可以透過範例檢查關鍵字的使用方式並了解流程的工作原理。

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of A: ");
int a = sc.nextInt();
//Scanner b = new Scanner(System.in);
System.out.println("Enter value of B: ");
int b = sc.nextInt();
try {
c= a/b;
if(b==0)
{
throw new ArithmeticException();
}
System.out.println("Value of C is: " +c);
}
catch(ArithmeticException e) {
System.out.println("Caught this here");
}
finally {
}
System.out.println("Finally block is here");
}
}

讓我們用一個經典且簡單的除以零的範例來解決這個問題。

分析下面的程式碼:

  • 首先,我們導入了 Scanner 模組來取得使用者輸入的值。
  • 使用者輸入兩個數字,即‘A’和‘B’。
  • 我們聲明了一個變數「C」來儲存數字「A」除以「B」後的商值。
  • 由於使用者可以隨機給出數字,我們可以想到使用者可以將除數設為零的情況。
  • 接受這種情況,我們在 try 區塊中編寫除法程式碼。
  • 如果條件是「B」值為零,那麼我們將拋出異常。
  • 總是在「try」後面加上 catch 區塊。
  • 所以,這裡我們提到了我們使用新指令透過關鍵字 throw 定義的例外。
  • 同樣的異常也被catch了,我們只是列印了下面的一條語句,以便清楚地了解try、throw、catch、finally的流程。
  • 然後,最後,我們宣布我們的finally 區塊。眾所周知,該區塊中的語句肯定會執行。

讓我們檢查下面的輸出。

輸出 1: 如果 b 值不為零。

Java 中的 throw 關鍵字

由於「B」值非零,所以沒有執行 try、throw 和 catch 區塊。最後,無論是否建立異常,都會執行該區塊。

輸出 2: 如果 b 值為零。

Java 中的 throw 關鍵字

突出顯示的部分確保異常被成功拋出並被 catch 區塊捕獲。

那麼,讓我們來看下面的例子。

範例:

讓我們看看如何在單一程式中拋出多個異常。

public class Main
{
public static void main(String[] args) {
String sun[] = {"chocolate", "honey", "sugar", "sweet", "bitter"};
String h = null;
try {
for (int i=0; i<=7; i++)
{
if(i > sun.length)
{
throw new ArrayIndexOutOfBoundsException();
}
System.out.println(sun[i]);
}
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println("Inside array index out of bounds exception catch block");
}
try{
sun[0]=h;
if (sun[0] == null)
{
throw new NullPointerException();
}
System.out.println("Value of sun[0] is: " +sun[0]);
}
catch(NullPointerException f)
{
System.out.println("Caught Null point exception");
}
finally {
System.out.println("executing finally");
}
}
}

上面,我們使用了兩種不同類型的異常和 throw 關鍵字來解釋程式。數組索引越界和空指標異常是我們在單一程式中使用的。

輸出:

Java 中的 throw 關鍵字

投擲的重要性

這個 throw 關鍵字將幫助我們充當 try 和 catch 區塊之間的橋樑。它將有助於將程式的控制權從 try 區塊轉移到 catch 區塊。

讓我向您展示一個程式如何使用和不使用 throw 關鍵字的範例。

public class Main
{
public static void main(String[] args) {
try {
int a = Integer.parseInt ("Happy") ;
System.out.println("Will this get printed?");
} catch(NumberFormatException e) {
System.out.println("Number format exception of catch block");
}
System.out.println("Priting after catch block");
}
}

我們寫的程式碼沒有 throw 關鍵字。但是我們使用程式碼來嘗試捕獲該區塊,該區塊將處理異常。那麼,你知道上面的輸出嗎?

Java 中的 throw 關鍵字

意料之中吧?因此,它執行了程式碼,發現了異常,並且捕獲了異常。

現在,當我們插入 throw 語句時,程式碼如何運作?下面就來看看吧。

public class Main
{
public static void main(String[] args) {
try {
int a = Integer.parseInt ("Happy") ;
throw new NumberFormatException();
System.out.println("Will this get printed?");
} catch(NumberFormatException e) {
System.out.println("Number format exception of catch block");
}
System.out.println("Priting after catch block");
}
}

只有被反白的部分是上面兩段程式碼之間的變化。

輸出:

Java 中的 throw 關鍵字

是的,我們有一個編譯錯誤,因為 throw 關鍵字後面的 print 語句無法存取。我們希望您透過這個範例準確地理解「將控制權從 try 轉移到 catch 區塊」的含義。

作為練習,嘗試刪除拋出關鍵字後的 print 語句並檢查程式如何反應。

結論

因此,這就是異常處理時 throw 指令的作用。我確實注意到 THROW 和 THROWS 關鍵字之間有很大的區別。兩者都與例外的概念一起使用。我們已經知道這個概念、如何以及在哪裡可以使用「THROW」關鍵字。只需練習並嘗試以不同的方式使用它。繼續學習。

以上是Java 中的 throw 關鍵字的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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