Custom Exceptions

Another powerful feature of Fault Manager is the ability to generate and compile exception classes on the fly.

Instead of spending time on building your own exception classes and end up with a pile of exception files here and there in your project, you can use Fault Manager and provide a name of an exception that has not been generated yet, and Fault Manager will do the rest. You actually don’t even have to do it before you throw the exception, you can generate exception classes just at the point you want to use them.

Take the following as example:

Example 7 Generate Custom Exception
Fault::throw('CustomError', 'a message here');

CustomError class is not yet defined anywhere in our project, what Fault Manager will do is:

Check if a class with that name already exists

If it is
  1. Get a new instance of the class with params

If it is not
  1. Generate the class and compile it to file

  2. Load the newly generated class with params

The above example will produce the following file:

Example 8 CustomError.php
<?php

class CustomError extends Exception
{


}

The above file will be saved into the compiled path directory.

For more info on how to define your own path, check Compile Path.

Rules

There are few rules to follow in order to generate a custom exception class:

  1. If you pass an empty string as $exceptionClass a ExceptionNameIsEmpty exception will be thrown

  2. If you try to generate a namespaced custom exception a NamespaceError exception will be thrown as that feature is not yet supported