Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤和異常

Java 多執行緒

Java 同步

Java 網路

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java API 和框架

Java 類參考

Java 有用資源

Java - throw 關鍵字



異常(或異常事件)是在程式執行期間出現的錯誤。當出現異常時,程式的正常流程被打斷,程式/應用程式異常終止,這是不建議的,因此,這些異常需要處理。

異常可能由於許多不同的原因發生。以下是發生異常的一些場景。

  • 使用者輸入了無效資料。

  • 需要開啟的檔案找不到。

  • 網路連線在通訊過程中斷開,或者 JVM 記憶體不足。

其中一些異常是由使用者錯誤引起的,另一些是由程式設計師錯誤引起的,還有一些是由以某種方式發生故障的物理資源引起的。

Throws/Throw 關鍵字

如果方法不處理已檢查異常,則該方法必須使用throws關鍵字宣告它。throws 關鍵字出現在方法簽名末尾。

您可以使用throw關鍵字丟擲異常,無論是新例項化的異常還是剛剛捕獲的異常。

嘗試理解 throws 和 throw 關鍵字之間的區別,throws用於推遲已檢查異常的處理,throw用於顯式呼叫異常。

以下方法宣告它丟擲 RemoteException -

語法

public static void accesElement() throws CustomException {
   try {
      int a[] = new int[2];
      System.out.println("Access element three :" + a[3]);
   } catch(Exception e) {
      throw new CustomException(e);
   }
}

示例

在此示例中,我們呼叫了一個名為 accessElement() 的方法,該方法宣告丟擲 CustomException。因此,我們需要在 accessElement() 方法上編寫一個 try-catch 塊。當程式執行時,它會丟擲異常並打印出來。

// File Name : ExcepTest.java
package com.tutorialspoint;

public class ExcepTest {

   public static void main(String args[])  {
      try {
         accesElement();
      } catch (CustomException e) {		
         e.printStackTrace();
      }
      System.out.println("Out of the block");
   }

   public static void accesElement() throws CustomException {
      try {
         int a[] = new int[2];
         System.out.println("Access element three :" + a[3]);
      } catch(Exception e) {
         throw new CustomException(e);
      }
   }
}

class CustomException extends Exception{
   private static final long serialVersionUID = 1L;
   CustomException(Exception e){
      super(e);
   }
}

輸出

com.tutorialspoint.CustomException: java.lang.ArrayIndexOutOfBoundsException: 3
	at com.tutorialspoint.ExcepTest.accesElement(ExcepTest.java:20)
	at com.tutorialspoint.ExcepTest.main(ExcepTest.java:8)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 3
	at com.tutorialspoint.ExcepTest.accesElement(ExcepTest.java:18)
	... 1 more
Out of the block

以下是另一個展示 throws 關鍵字用法的示例。

示例

在此示例中,我們呼叫了一個名為 accessElement() 的方法,該方法宣告丟擲 CustomException。因此,我們沒有在 accessElement() 方法上編寫 try-catch 塊,而是 main 方法宣告丟擲異常。當程式執行時,它會丟擲異常並打印出來。

// File Name : ExcepTest.java
package com.tutorialspoint;

public class ExcepTest {

   public static void main(String args[]) throws CustomException  {
      accesElement();
      System.out.println("Out of the block");
   }

   public static void accesElement() throws CustomException {
      try {
         int a[] = new int[2];
         System.out.println("Access element three :" + a[3]);
      } catch(Exception e) {
         throw new CustomException(e);
      }
   }
}

class CustomException extends Exception{
   private static final long serialVersionUID = 1L;
   CustomException(Exception e){
      super(e);
   }
}

輸出

Exception in thread "main" com.tutorialspoint.CustomException: java.lang.ArrayIndexOutOfBoundsException: 3
	at com.tutorialspoint.ExcepTest.accesElement(ExcepTest.java:16)
	at com.tutorialspoint.ExcepTest.main(ExcepTest.java:7)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 3
	at com.tutorialspoint.ExcepTest.accesElement(ExcepTest.java:14)
	... 1 more

由於異常未被處理,因此在此示例中最後一個語句未執行。

一個方法可以宣告它丟擲多個異常,在這種情況下,這些異常以逗號分隔的列表的形式宣告。例如,以下方法宣告它丟擲 RemoteException 和 InsufficientFundsException -

示例

import java.io.*;
public class className {

   public void withdraw(double amount) throws RemoteException, 
      InsufficientFundsException {
      // Method implementation
   }
   // Remainder of class definition
}
java_basic_syntax.htm
廣告

© . All rights reserved.