A simple exception example - PHP

I am trying to figure out which 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 that it would be nice to throw an exception in the class, and then use only one catch block in all the scripts that the employee object will use. But this does not seem to work. I need to have a catch try block inside a class - is this the right way to look at this?

thanks

+4
source share
2 answers

I think you say you want to do something like this:

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

... and then be able to impose it in other classes without explicitly throwing any exceptions:

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

You cannot do this because then the try / catch block in the class will catch only the exceptions that occur when the class is defined. They will not be caught when trying to instantiate, because your new Employee line is outside the try / catch block.

So your problem is that you want to be able to reuse the try / catch block in several places without re-writing the code. In this case, the best solution is to move the contents of the catch block into a separate function, which you can call as needed. Define the function in the Employee class file and call it as follows:

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

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

+4
source

You should learn more about Exceptions in PHP .

You can handle exceptions in class methods, of course. But you have to rethink how you want to do this, and ... why.

Good practice also creates your own exception class, so you can distinguish the exceptions that you selected with your module / class from the exceptions thrown by something else. It looks like this ( see more ):

 class EmployeeModule_Exception extends Exception {} 

and when it comes to throwing an exception:

 // the second parameter below is error code throw new EmployeeModule_Exception('some message', 123); 

Catching is similar, only the example below will catch only the exceptions of your module:

 try { // some code here } catch (EmployeeModule_Exception $e) { // display information about exception caught echo 'Error message: ' . $e->getMessage() . '<br />'; echo 'Error code: ' . $e->getCode(); } 
+4
source

All Articles