Extending Exceptions

A User defined Exception class can be defined by extending the built-in
Exception class. The members and properties below, show what is accessible
within the child class that derives from the built-in Exception class.

If a class extends the built-in Exception class and re-defines the constructor, it is highly recommended
that it also call parent::__construct()
to ensure all available data has been properly assigned. The __toString() method can be overridden
to provide a custom output when the object is presented as a string.

Note:

Exceptions cannot be cloned. Attempting to clone an Exception will result in a
fatal E_ERROR error.

Custom exception classes can allow you to write tests that prove your exceptionsare meaningful. Usually testing exceptions, you either assert the message equalssomething in which case you can't change the message format without refactoring,or not make any assertions at all in which case you can get misleading messageslater down the line. Especially if your $e->getMessage is something complicatedlike a var_dump'ed context array.

The solution is to abstract the error information from the Exception class intoproperties that can be tested everywhere except the one test for your formatting.

function testExceptionFormattingOnlyOnce() {$e = new TestableException;$this->assertEquals('I have formatted: properly for the only required test!!',$e->format('properly for the only required test') );}

I have written similar simple custom exception class. Helpful for newbie.
<?php
/*
This is written for overriding the exceptions.
custom exception class
*/
error_reporting(E_ALL-E_NOTICE);
class myCustomException extends Exception
{