Is this legal? Not near my work computer so can't test
try
someClass::someStaticFunction();
catch(Exception $e)
$e->getMessage();
Specifically the lack of brackets, similar to an if-else statement
if(someBool)
someClass::someStaticFunction();
else
someClass::someOtherFunction();
Cheers!
The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.
PHP allows a series of catch blocks following a try block to handle different exception cases. Various catch blocks may be employed to handle predefined exceptions and errors as well as user defined exceptions.
catch” syntax. It works like this: First, the code in try {...} is executed. If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch .
If you've one if/else block instead of one try/catch block, and if an exceptions throws in the try/catch block, then the if/else block is faster (if/else block: around 0.0012 milliseconds, try/catch block: around 0.6664 milliseconds). If no exception is thrown with a try/catch block, then a try/catch block is faster.
No you must use the brace brackets. Note that you can catch exceptions by type :
try {
someClass::someStaticFunction();
} catch (MyException $e) {
// do specific stuff for this exception
} catch (Exception $e) {
// do stuff for other exceptions
}
No, doesn't seem like it works.
Check my example here: http://codepad.org/BpSBiPDY
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With