class Entity {
}
class EntityException extends \Exception {
private $entity;
public function getEntity() {
return $this->entity;
}
public static function create($entity, $message = 'Error', $code = 0)
{
$exception = new static($message, $code);
$exception->entity = $entity;
return $exception;
}
}
try {
throw EntityException::create( new Entity, 'Internal error' );
} catch(\EntityException $e) {
// Handle entity exceptions
echo $e->getMessage() . PHP_EOL;
var_dump($e->getEntity());
} catch(\Throwable $e) {
// Handle other exceptions
}