Hi,
How to treat exception in best way in construct?
option1 - catch exception where object created:
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();
}
}
option2: catch exception inside __construct
class Account{
function __construct($id){
try{
if(empty($id)){
throw new My_Exception('id can\'t be empty');
}
// ...
}
catch(My_Exception $e) {
}
}
Please write in which cases should be used option1 and in which should be used option2 or other better solution.
Thanks