Skip to content
Back to skills

Exception

ASecurity

Do not throw exceptions from pure code with error or throw, give exceptions structured types, report failures with MonadThrow instead of returning Either, and never silently swallow errors in IO. Use when writing or reviewing Haskell error handling, exceptions, validation or parsing functions, or IO code.

  • 3 stars
  • 0 votes
  • 0 copies
  • 8 views
  • Added May 27, 2026
data-airust

Security analysis

A100/100

Scanned December 3, 2026

npx -y skills add ncaq/konoka --skill exception --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Exception?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Exception
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ncaq-exception/badge)](https://www.skillsdirectory.com/skills/ncaq-exception)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: exception
description: Do not throw exceptions from pure code with error or throw, give exceptions structured types, report failures with MonadThrow instead of returning Either, and never silently swallow errors in IO. Use when writing or reviewing Haskell error handling, exceptions, validation or parsing functions, or IO code.
user-invocable: false
---

# 例外処理の原則

## 純粋なコードから例外を投げない

`error`は原則として使いません。
純粋なコードから投げた例外は呼び出し側で捕捉するのが難しく、
中身が`String`で構造を持たないのでデバッグもしにくいためです。
捕捉が難しい理由は[haskell-tasuke:partial-function](../partial-function/SKILL.md)にも書いています。

`throw`(導入されていれば、非純粋であることを名前で示す`impureThrow`)は、
例外に型が付く点では`error`よりましですが、
純粋なコードから例外を投げる点は同じなので、やはり推奨しません。

エラーの情報は、後述のように`MonadThrow`で呼び出し側に伝播させます。

論理的に起こり得ない場所に限り、純粋なコードから例外を投げて構いません。
そこに到達するほどおかしなことが起きているなら、
アプリケーションごと終了させるのが適切だからです。
その場合も、なるべく例外の型を作って投げます。

## `undefined`はPRを出す前に取り除く

`undefined`は、Rustの`todo!`と同じように、開発中の仮の実装として使って構いません。
例えばテストを先に書くときに、
関数の本体を`undefined`で埋めてコンパイルだけ通しておけます。

`undefined`も純粋なコードから例外を投げるので、PRを出す前に全て取り除きます。
残っていれば警告が出るはずです。

## 例外に型を付ける

`throwString`のような関数は使わず、例外の型を定義して`throwM`などで投げます。

例外の型のフィールドには、`Text`などの文字列型ではなく、
なるべく状況を表す構造的なデータ型を持たせます。
循環importが起きる場合は、例外を投げる側で`Text`に変換して構いません。

## 失敗は`Either`より`MonadThrow`で伝える

失敗する可能性のある関数を定義するときは、
`Either e a`を返すのではなく、
なるべく`(MonadThrow m) => ... -> m a`の型にして`throwM`で失敗を伝えます。
検証やパースの関数を新しく書くときに特に当てはまります。

```haskell
-- 避ける
parseArchitecture :: Text -> Either UnsupportedArchitecture Architecture

-- 良い
parseArchitecture :: (MonadThrow m) => Text -> m Architecture
parseArchitecture "x86_64" = pure X86_64
parseArchitecture "aarch64" = pure Aarch64
parseArchitecture other = throwM $ UnsupportedArchitecture other
```

`IO`などの文脈から呼ぶと`m`がその文脈になるので、
結果をそのまま束縛でき、`either throwM pure`のような変換が要りません。
`m`を`Maybe`や`Either SomeException`にすれば純粋なコードからも呼べます。
その場合は例外は投げられず`Nothing`や`Left`になるので、
純粋なコードから例外を投げないという原則とも両立します。
特定の例外型で分岐したい呼び出し側は、
`try`や`catch`でその型を指定して捕まえます。

失敗が例外的な状況ではなく、呼び出し側が必ず分岐して処理する正常系であれば、
後述のデータベースの例のように`Maybe`や`Either`を返して構いません。

テストでは`m`を`IO`にして、
`try`で例外型を捕まえ、得られた`Either`を期待する値と直接比較します。
そのために例外型には`Eq`のインスタンスも導出しておきます。
直接比較する理由は[haskell-tasuke:test-equal](../test-equal/SKILL.md)に書いています。

```haskell
try (parseArchitecture "sparc") `shouldReturn` Left (UnsupportedArchitecture "sparc")
```

## エラーを握り潰さない

`IO`の文脈などで例外が起きたときに、捕捉して何もしないコードは書きません。
`IO`は例外が起こり得ることを既に示している文脈で、
例外が上位へ伝播することは許容されています。
握り潰すくらいなら、捕捉せずに上位に任せるほうが概ね適切です。

- その場で処理するのが適切だと言い切れる例外は、問題の程度に応じたログを出して処理します。
- 捕捉してもデバッグが楽になるだけで解決はできない例外は、再送出します。

`Either`の`Left`なども例外と同じように扱います。
対処できる`Left`にはデフォルト値やフォールバック値を使い、
想定外で対処できない`Left`は`MonadThrow`の文脈に載せて上位に任せます。

## `IO`の文脈では`Maybe`や`Either`で包むより例外を投げる

`IO`の文脈で例外的な状況を表すときは、
`Maybe`や`Either`で包んで返すより、素直に例外を投げるほうが良いでしょう。
`IO`が例外の可能性を既に表しているので、二重に包むと混乱を招くためです。
基底モナドなどで`IO`の操作をしているが`IO`そのものではないモナドでは、
`MonadThrow`や`MonadIO`の型クラスを使います。

データベースやネットワークからデータを取得する操作では、
存在しないことは例外的な状況ではなく、頻繁に起きる正常系です。
この場合は`IO (Maybe a)`のようなシグネチャが分かりやすく適切です。

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in data-ai →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.