# How should I design the exception hierarchy in a library so consumers can catch cleanly?

> Define a root marker interface extending Throwable, mark recoverable errors with a thin RetryableException, and never throw a bare Exception.

- Asked: 2026-11-18
- Answered: 2026-11-20
- Tags: PHP, API Design
- Source: https://muhammetsafak.com/just-ask/design-exception-hierarchy-library-so-consumers-catch-cleanly/
- Language: en-US
- Author: Muhammet Şafak

---
**Question:** I'm authoring an SDK and I'm stuck on exception design. I want consumers to be able to catch a single thing (a marker interface) and say "this error came from my library".

But at the same time I want the caller to distinguish a recoverable error (a transient network issue, retryable) from a fatal one (misuse, a config error). How do I combine these two needs in one hierarchy in PHP? Nothing more, nothing less.


## Short answer

Define a marker interface at the root of your library and have every exception you throw implement it; then signal recoverable vs fatal with SPL base classes and, where needed, a thin sub-marker.

## Context

PHP's interfaces are exactly what let you combine both in one class.

## Why

1. **Let the root marker extend `\Throwable`.** Declare `interface AcmeException extends \Throwable {}`. Every exception you throw implements it, so the caller can write `catch (AcmeException $e)` in one line and say "this is from my SDK". This is the approach the PSRs adopted too.

2. **Have concrete classes extend the right SPL base.** That way they carry both your marker and the [standard hierarchy](https://muhammetsafak.com/blog/exception-and-error-handling-in-php/). `LogicException` is a programmer error (misuse, config — fatal, don't retry); `RuntimeException` is a runtime/environmental error (network, timeout — often recoverable). That split already gives you half the recoverable/fatal signal.

## What to do

1. **Mark recoverable with a separate thin marker.** Rather than burying "retryable" in a base class, turn it into an explicit contract: `interface RetryableException extends AcmeException {}`. The caller catches only that, backs off and retries; it treats everything else as fatal.

   ```php
   interface AcmeException extends \Throwable {}
   interface RetryableException extends AcmeException {}

   // Transient — the caller may retry
   final class ConnectionException extends \RuntimeException implements RetryableException {}

   // Permanent — misuse/config, retrying is pointless
   final class InvalidConfigException extends \InvalidArgumentException implements AcmeException {}
   ```

2. **Never throw a bare `\Exception` or `\RuntimeException`.** Everything leaving your library must carry the marker, or the caller's single-catch promise is broken. When you catch a lower layer's exceptions (Guzzle, PDO) and wrap them in your own type, preserve the chain with `$previous` so diagnostics aren't lost.

3. **Carry context as typed properties/getters.** Don't make callers parse the message; expose getters like `->getStatusCode()`, `->getRetryAfter()`. When the caller decides what to do, let it look at the type and the data, not a string.

4. **Don't over-engineer.** Start with the marker + Retryable + a few concrete types. Add classes as the need appears; because you expose the contract through interfaces, splitting concrete classes later won't break callers.

**Bottom line:** personally I'd build three layers: a root `AcmeException` (marker), a `RetryableException` (the recoverable contract), and concrete classes extending the correct SPL base (`Logic` vs `Runtime`). The caller can catch broadly (`AcmeException`) or catch only the recoverable ones and retry. That satisfies both your "single marker" wish and the recoverable/fatal distinction without adding overhead.

*Compiled from questions I have run into in the field or been asked.*

## Related Reading

- [Exception and Error Handling in PHP](https://muhammetsafak.com/blog/exception-and-error-handling-in-php/) — Blog
- [Should I use JSON Merge Patch or JSON Patch for partial updates?](https://muhammetsafak.com/just-ask/use-json-merge-patch-json-patch-partial-updates/) — Just Ask
- [How do I keep reflection cost under control in an attribute-based mapping layer in production?](https://muhammetsafak.com/just-ask/keep-reflection-cost-under-control-mapping-layer-production/) — Just Ask
- [How can I evolve my API in a backward-compatible way without cutting a new version?](https://muhammetsafak.com/just-ask/evolve-api-backward-compatible-without-cutting-new-version/) — Just Ask
