php - Better way of error handling? -
what best way of error handling? came with:
class test { public static function payment($orderid, $total) { if (empty($orderid) && empty($total)) { return array('status' => 'fail', 'error' => 'missing data'); } } }
i heard try/exceptions how fit code? if provide example great!
as mentioned, use exceptions. specific example, throw
exception if condition fails. when envoke method can throw exception, wrap try/catch
handling block.
class test { public static function payment( $orderid, $total ) { if (empty( $orderid ) && empty( $total )) { throw new exception('missing data'); } } } try { test::payment("1", "2"); //should fine test::payment(); //should throw exception } catch (exception $e){ echo $e; //do other things if need }
Comments
Post a Comment