How to treat exceptions in constructor best?

b0ssX

New Member
How to treat exception in best way in construct?option1 - catch exception where object created:\[code\]class Account { function __construct($id){ if(empty($id)){ throw new My_Exception('id can\'t be empty'); } // ... }}class a1 { function just($id){ try { $account = new Account($id); } catch(Exception $e){ $e.getMessage(); }}class a2{ function just($id){ try { $account = new Account($id); } catch(Exception $e){ $e.getMessage(); }}\[/code\]option2: catch exception inside __construct\[code\]class Account{ function __construct($id){ try{ if(empty($id)){ throw new My_Exception('id can\'t be empty'); } // ... } catch(My_Exception $e) { }}\[/code\]Please write in which cases should be used option1 and in which should be used option2 or other better solution. Thanks
 
Back
Top