PHP Exception handler

16

Click here to load reader

description

How use PHP Exceptions ?

Transcript of PHP Exception handler

Page 1: PHP Exception handler

PHP Exception handler

Throwing and catching exceptions is the good way to make sure your application doesn't do stuff it isn't supposed to do and leaving your application broken.

Page 2: PHP Exception handler

ISSUE ??

When you're working on a web application, this might not be your desired action because you don't want to let your visitor know your application is broken. Instead, you want to handle exceptions your own way, by bubbling exceptions upwards so you can manage exceptions in one place.

Page 3: PHP Exception handler

Bubbling exceptions ??

Bubbling exceptions means that you check in your code fragment for an exception (in your try statement) and throwing this upwards (in your catch statement).

Page 4: PHP Exception handler

Exmp.

class MMMException extends Exception {}

function sendMailers()

{

try {

// send MMM Mailers

} catch (MMMException $e) {

throw new MMMException($e->getMessage());

log_message('error', 'Some happen.');

}

return $this->xmlrpc->send_error_message('001', 'error');

}

Page 5: PHP Exception handler

So Finally ...

You can create your exception handler method to handle each exception on it's own and perform a specific action. In this case you might want to log these exceptions, but when accessing databases, files or webservices you might want to send out an e-mail to the administrator or developers.

Page 6: PHP Exception handler

Wrapping it up ...

Throwing a default ‘Exception’ is bad practice if you ever want to act on that specific error scenario. Without extending the Exception class you can only catch all or none.

New exceptions can be thrown in catch blocks. That way it’s possible to prevent unexpected exception types to cross certain application design boundaries.

Once class design involves abstract classes or interfaces it is wise to design exception structures as well and organize them in similar layers of abstraction.

Page 7: PHP Exception handler

Sample code

Let's start right to see sample code to emulate exception (Everyone loves code, right?)

Page 8: PHP Exception handler

At any point where a developer needs to handle the possibility of an exception being thrown he needs to know:-

What Exceptions can I expect? What Exceptions do I plan to catch?

Page 9: PHP Exception handler

The Basic Exception

Let’s assume a XMLRPC-based web-service client class used to submit order-data to a supplier’s web-service. The web-service client throws an exception if required configuration parameters are missing. The actual XMLRPC Client object will be initialized when the first webservice method is called.

Page 10: PHP Exception handler

The Basic Exception

try {

$client->submitOrder($order);

} catch (Exception $e) {

// We need to act on this asap...

$AppError->register_error($e);

// Redirect to application error page

$Redirect->error($e);

}

Page 11: PHP Exception handler

The Basic Exception

In case of a configuration error a redirect is performed to an application-error page. And (just an example) some mechanism is triggered that starts to bug a (or all) developers that there’s a problem that needs immediate attention.

Page 12: PHP Exception handler

Knowing what to catch

class WsXMLRPC_ClientException extends Exception {}

class WsXMLRPC_ClientConfigurationException extends WsXMLRPC_ClientException{}

class WsXMLRPC_ClientConnectionException extends WsXMLRPC_ClientException{}

Page 13: PHP Exception handler

class WsXMLRPC_Client

{

// ...

private function initXMLRPC_Client() {

if (!is_null($this->XMLRPC_Client)) {

return;

}

if (!$this->config->searchAgent) {

throw new WsXMLRPC_ClientConfigurationException(

'Configuration error'

);

}

try {

$this->XMLRPC_Client = new XMLRPC_Client(

$this->config->searchAgent,

array('exceptions'=>1)

);

} catch (XMLRPC_Fault $e) {

throw new WsXMLRPC_ClientConnectionException(

'Cannot load SearchAgent: '.$this->config->searchAgent

);

}

}

}

Page 14: PHP Exception handler

Sample Code ...

$client = new WsXMLRPC_Client($config);

try {

$client->submitOrder($order);

} catch (WsXMLRPC_ClientConnectionException $e) {

// store the order in a queue to be processed later

$Order->queue();

$Redirect->Page('OrderQueued', $order);

} catch (Exception $e) {

// Catch everything, also WsXMLRPC_ClientConfigurationException

// We need to act on this asap...

$AppError->register_error($e);

// Redirect to application error page

$Redirect->error($e);

}

Page 15: PHP Exception handler

Exception in batch job do {

$retry = false;

try {

$bool = // return true if send mail

if ($bool === false) {

throw new MailSendException();

}

}

catch (MailSendException $e) {

if (Not found SMTP Detail) {

echo "$mailerid SMTP Details" ."ERROR.\n";

}

else {

if (Connection timeout error) {

echo "connection time out error" ."$mailerid.\n";

}

else {

echo "Unknown error attempting to access " ."$mailerid.\n";

}

}

$retry = true;

}

}

while ($retry);