# 例外処理

「どのようなときに例外を使うか？」についての一つの考え方です。

* 例外を使用する／しない

  通常の処理フローで発生する結果については、例外は使用しません。

例をいくつか挙げます。

* ある条件で検索したところ、検索結果が0件だった場合、これは予期される通常の処理フローなので例外は使用しない

  一覧表示された項目の１つを選んで詳細表示をしたら、データが取れなかった場合、これは予期できない処理フローなので例外を使用する

関数に渡された引数が不正（有効範囲外）であった場合、これは予期できない処理フロー（バグ）なので例外を使用する

ユーザが画面から入力したデータが不正（有効範囲外）であった場合、これは予期できる通常の処理フローなので例外は使用しない

* 例外処理をする／しない

  ある関数を呼び出したとき、その関数処理内で例外が発生する可能性がある場合、必ず例外処理（try-catch）を書かなくてはならないのでしょうか？

例外から回復する可能性がある場合、回復処理を書くために例外を捕捉（try-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://yutakatay.gitbook.io/katapedia/doc/li-wai-chu-li.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.
