Closed9

Scala (Java) のエラー型の基本をおさらい

RikiyaOtaRikiyaOta

↑これは全部 Java のもの。Scala らしくエラーハンドリングするなら、Option, Either を使っていればよろしい。

RikiyaOtaRikiyaOta

Throwable

https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html

先ほどの図の通り、全てのエラーを表す型の親クラスになるもの。

Exception

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Exception.html

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

catch してハンドリングすることを意図したエラー型。

The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions.

Exception のサブクラスのうち、RuntimeException のサブクラスでないものはチェック例外。
(逆にいうと、RuntimeExceptionはチェック例外でない)

Error

https://docs.oracle.com/javase/8/docs/api/java/lang/Error.html

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

catch すべきでないエラー。なので、異常事態を表すエラーっぽい。

RikiyaOtaRikiyaOta

他の scala が提供するエラー周りのクラスも見ておく。

このスクラップは6ヶ月前にクローズされました