Custom exceptions

A custom exception is a class that extends Exception and carries business meaning, for example StockInsuffisantException. It lets you distinguish errors and handle them precisely with separate catch blocks, and can carry context data.

Example

class StockInsuffisant extends Exception {}
throw new StockInsuffisant('Plus de stock');

Related concepts

The whole glossary