# 認識例外類別

於「例外」該主題開始，使用的範例中提到 java.lang.ArrayIndexOutOfBoundsException\
例外類別，那麼本篇將列出較常見的一些例外類別：

繼承來自java.lang.Throwable 類別的 兩大類， java.lang.Error 與 java.lang.Exception

Error 是用來處理會嚴重影響程式的錯誤，所以我們不會去捕捉這類的錯誤。\
Exception 為一些較一般的例外，而且我們可以做適當的處理使程式繼續執行。

![](/files/-MPaKml_nWpGgZ5bc9N3)

有趣的是，如果拋出的例外是 RuntiomeException 類別，或是其下方的子類別，\
就算不撰寫例外處理的程式敘述，依然是可以 編譯成功。 （如「例外」該主題開始，使用的範例）

相反的，如果拋出為 IOException 是必須撰寫例外處理的程式敘述。

所以當例外發生，catch() 接收 Throwable 子類別產生的物件。\
在 [上篇例外處理的範例](/java-14/java-14-3.md) 文末提到了

```
catch(ArrayIndexOutOfBoundsException e)
```

catch() 括號內只會接收子類別產生的物件，其他物件是不會接收的。\
而這個 e 我們從上方的流程關係圖可以得知，\
Throwable -> Exception -> RuntimeException\
-> IndexOutOfBoundsException -> ArrayIndexOutOfBoundsException 所建立的物件。

於下篇將會談，那麼 catch() 括號內只接收一種類別，如何捕捉多個例外？


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://java.4-x.tw/java-14/java-14-4.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
