Skip to content

Exception handling

Greg Bowler edited this page Apr 17, 2026 · 1 revision

This library exposes two exception types.

Both are runtime exceptions, and both follow the PSR container exception interfaces.

ServiceContainerException

ServiceContainerException is the general-purpose container exception type. It implements Psr\Container\ContainerExceptionInterface.

You will most commonly see it when invalid values are passed to Container::set():

$container->set("not an object");

Only objects may be registered directly. If a string, array, integer, or null is supplied, this exception is thrown with a message explaining the invalid type.

ServiceNotFoundException

ServiceNotFoundException extends ServiceContainerException and implements Psr\Container\NotFoundExceptionInterface.

It is thrown when the container cannot resolve a requested type:

$container->get(Greeter::class);

The same exception may be raised indirectly by Injector when invoking a callable that requires a non-nullable service the container does not have.

When exceptions are not thrown

There are two cases where missing services do not raise an exception:

  • Container::has() returns false instead of throwing.
  • Injector injects null for a missing service when the parameter type is nullable.

These two behaviours make it possible to model genuinely optional services without special container configuration.

Clone this wiki locally