Index
# Exceptions **Exceptions** are **events**, which occurs during the execution of a program, that **disrupts** the normal flow of the program's instructions. You MUST throw exception rather than using the php `trigger_error` mechanism. ## Get started **Exceptions** in the framework define two concepts: - An exception has an **origin**, the object that throws the error. In case of functions throwing exceptions, the origin is the function's name. - An exception has a **context**, a map of values. The context is injected in the message through moustache placeholders : `{xxx}`. Any value in the context is printed by the current application <@\MMO\core\printers\Printer>. By default, the context already holds the **origin** so `{origin}` will be replaced by its textual representation. ## Usage ### Basic ```php $obj = new stdClass(); //simple throw : "stdClass: critical." throw new MMO\core\exceptions\eRuntime($obj, 'critical.'); //throw with context: "stdClass: critical condition." throw new MMO\core\exceptions\eRuntime($obj, 'crititical {error}.', ['error' => 'condition']); ``` ### Inside a function ```php function test() { throw new MMO\core\exceptions\eRuntime(__FUNCTION__, 'critical.'); } ``` ### Inside a class ```php class Test { public function test() { throw new MMO\core\exceptions\eRuntime($this, 'critical.'); } } ``` ## Implementing new exceptions - A custom exception MUST implement the interface <@\MMO\core\interfaces\iException>. - Its name MUST starts with the lower case `e` - You CAN also use the trait <@\MMO\core\classes\tException> so you can extend an another exception class. ```php class eMyException extends \LogicException implements \MMO\core\interfaces\iException { use \MMO\core\classes\tException; } ``` ## References ### See also - [printers](Printers.md) - <@\MMO\core\exceptions\*> ## Tags <#exception>, <#printer>