Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Exception sample - PHP

Tags:

oop

php

I am trying to understand what the best approach would be to handle Exceptions in the following scenario:

I have a class employee:

class employee extends person {
    private $salary;
    private $baseSalary = 6.5;

    function __construct($f, $m, $l, $a,$fsalary=0){
        if(!is_numeric($fsalary)){
            throw new Exception("Age supplied is not a number", 114);
        }
        parent::__construct($f, $m, $l, $a);    
        $this->salary=$fsalary;
    }
    function GetDetails(){
         return parent::GetName().
                "<br/>".
                $this->salary;
    }
    function __toString(){
        return $this->GetDetails();
    }

}

And using this:

try{
    if(!$f = new employee("Sarah", "Sebastian", "Pira", "abc")){
        throw new Exception();
    }
    else {
        echo $f;        
    }

}
catch (Exception $e){
    echo "<br/>";
    echo var_dump($e);
}

Now I would think it would be a good idea to throw an exception in the class and then use just one catch block in all the scripts that would be using an employee object - But this doesn't seem to work - I need to have a try catch block within the class - Is this the correct way of looking at this?

Thanks

like image 427
digital_paki Avatar asked Feb 25 '23 07:02

digital_paki


1 Answers

I think what you're saying is that you want to do something like this:

try {
    class Employee extends Person {
        // ...blah blah...
    }
}
catch(Exception $e) {
    // handle exception
}

...and then be able to insantiate it in other classes, without explicitly catching any exceptions:

// try { << this would be removed
    $employee = new Employee();
// }
// catch(Exception $e) {
//    (a whole bunch of code to handle the exception here)
// }

You can't do that, because then the try/catch block in the class will only catch any exceptions that occur when defining the class. They won't be caught when you try to instantiate it because your new Employee line is outside the try/catch block.

So really, your problem is that you want to be able to re-use a try/catch block in multiple places without re-writing the code. In that case, your best solution is to move the contents of the catch block out to a separate function that you can call as necessary. Define the function in the Employee class file and call it like this:

try {
     $employee = new Employee();
     $employee->doSomeStuff();
     $employee->doMoreStuffThatCouldThrowExceptions();
}
catch(Exception $e) {
    handle_employee_exception($e);
}

It doesn't get rid of the try/catch block in every file, but it does mean that you don't have to duplicate the implementation of the exception-handling all the time. And don't define handle_employee_exception as an instance method of the class, do it as a separate function, otherwise it will cause a fatal error if the exception is thrown in the constructor because the variable won't exist.

like image 149
p.g.l.hall Avatar answered Feb 26 '23 20:02

p.g.l.hall