Everyone here should know the \'or\' statemens, usually glued to an die() command:
$foo = bar() or die(\'Error: bar function return false.\');
You can also create a custom exception class and use it's static constructor method instead of throw new Exception() construction.
Exception class:
class CustomException extends Exception {
static public function doThrow($message = "", $code = 0, Exception $previous = null) {
throw new Exception($message, $code, $previous);
}
}
Usage:
try {
$foo = bar() or CustomException::doThrow('Problems with bar()');
$aa = bb($foo) or CustomException::doThrow('Problems with bb()');
} catch(Exception $e){
echo $e->getMessage();
}
Note
If you are using PHP 7 and higher - you can rename static method
doThrow()to simplythrow(), since in PHP 7 and higher it's allowed to use reserved keywords as method names.