32
Handling Error & Exception in PHP Presenter: Pravasini Sahoo, Mindfire Solutions Date: 16/07/2015

Handling error & exception in php

Embed Size (px)

Citation preview

Page 1: Handling error & exception in php

Handling Error & Exception in PHP

Presenter: Pravasini Sahoo, Mindfire SolutionsDate: 16/07/2015

Page 2: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Working as a PHP developer in MFS for more than 5 years. My skill is on PHP, MySQL, HTML, HTML5, CSS, CSS3, jQuery, AJAX, Javascript, XML, ZendFramework, GoogleAnalytics, YouTubeAPI, ZendFramework2, Twitter Bootstrap, JSON

Zend Certified Engineer (ZCE) - Zend PHP 5.3 Certification Oracle Certified Professional - OCP MySQL 5 Developer - Part 1 - 1Z0-871 Microsoft Certified Professional - MCP - Programming in HTML5 with Javascript & CSS3

Best Web Dev Article for month of January in CodeProject Speaker in GDG group (Srusti Academy of Management, Koustuv Group) Speaker in MSDC group (USBM) Speaker in GDG group (IIT Bhubaneswar on HTML5 & CSS3)

Page 3: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

What are Errors Types of PHP Errors Ways to handle PHP Errors What are Exceptions Handling Exceptions Default Exception handler Conclusion Questions!!!

Page 4: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Page 5: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

An Error is a term used to describe any issue that arises unexpectedly that cause a computer to not function properly.

Page 6: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Page 7: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

These are errors in which the code behaves unexpectedly due to some code in the program not acting as expected. Can use newrelic for finding and fixing problems in the server - https://docs.newrelic.com/docs/agents/php-agent/installation/php-agent-installation-ubuntu-debian Sentry : It is very useful for logging the errors/exceptions without writing the code manually. It will group the same error messages and will be logged in a nice manner - https://www.getsentry.com/welcome/

Page 8: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

E_ERROR E_WARNING E_NOTICE

E_USER_ERROR E_USER_WARNING E_USER_NOTICE

E_RECOVERABLE_ERROR E_ALL

Page 9: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

In general, these kind of errors can not be recoverable and it halts the execution of the running script.

undefinedFunction();

#Fatal error: Call to undefined function undefinedFunction() in /test-project/test.php on line 1

Page 10: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Generally, these errors are non-fatal runtime errors and can be recoverable. It does not halt the execution and also it indicates the system/user that some problem has happened.

include(“missing-file.php”);

#Warning: include(file.php): failed to open stream: No such file or directory in /test-project/test.php on line 1

#Warning: include(): Failed opening 'file.php' for inclusion (include_path='.:') in /test-project/test.php on line 1

Page 11: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

These type of error produces notices in run-time context. Generally, it happens when the script found something wrong which can show error while running the script.

$a = $b + 1;

#Notice: Undefined variable: b in /test-project/test.php on line 1

Page 12: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

These are like E_ERROR but set by the user programmatically using trigger_error().

if (function_exists(undefinedFunction)) { undefinedFunction();} else { trigger_error('The function you called, does not exists', E_USER_ERROR);}

#Notice: Use of undefined constant undefinedFunction - assumed 'undefinedFunction' in /test-project/test.php on line 1#Fatal error: The function you called, does not exists in /test-project/test.php on line 4

Page 13: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

These are like E_WARNING but set by the user programmatically using trigger_error().

if (include("file.php")) { include("file.php");} else { trigger_error('The file you are searching for not found', E_USER_WARNING);}

#Warning: include(file.php): failed to open stream: No such file or directory in /test-project/test.php on line 1#Warning: include(): Failed opening 'file.php' for inclusion (include_path='.:') in /test-project/test.php on line 1#Warning: The file you are searching for not found in /test-project/test.php on line 4

Page 14: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

These are like E_NOTICE but set by the user programmatically using trigger_error().

if (isset($b)) { $a = $b + 1;} else { trigger_error('$b is not defined yet');}

#Notice: $b is not defined yet in /test-project/test.php on line 4

Page 15: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Page 16: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Show the errors to users Log the errors Ignore them, as like nothing has happened Act on them and fix it

Page 17: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

display_errors = on/1 in php.ini file ini_set(‘display_errors’,1); in application config file php_value display_errors 1 in .htaccess file Set error_reporting() to the appropriate severity level

Page 18: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

log_errors = On in php.ini file error_log = path/to/file in php.ini file error_log(‘User Defined Error’); in your application Send the errors through email Log the errors in DB Log into physical file structure

Page 19: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Suppress Errors using @ operator Like @fopen or @file_get_contents

Page 20: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Using set_error_handler(<user_defined_error_handler>) It has 5 parameters, which is used to get proper error message

error_level error_message error_file error_line error_context

Page 21: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Page 22: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

In general, exception is a certain thing which is excluded from a general statement or does not follow the rule.

In computer language, Exception handling is the process of responding to the occurrence, during computation, often changing the normal flow of program execution.

Page 23: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Page 24: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

try —> Try to run the code which may throw exception throw —> Triggering an exception catch —> Handle the thrown exception finally —> Default code block to run, even after exception

Not available in PHP

Page 25: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Page 26: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

user-defined Exception Handler It handles any uncaught exception After handling, the script halts

function defaultExceptionHandler() {// code to handle exception}

set_exception_handler(‘defaultExceptionHandler’);

class CustomExceptionHandler() {public function handleException() {

// code to handle exception}}

set_exception handler(array(‘CustomExceptionHandler’, ‘handleException’));

Page 27: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Page 28: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Error or Exceptions are usual in PHP Logging is very important Do not ignore the errors If not handled properly, they will turn into bugs Always have a default error handler to log the errors and handle in an efficient way, even if we know our code will never show any error :)

Page 29: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Page 30: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

http://www.computerhope.com/jargon/e/error.htm

http://www.w3schools.com/php/php_exception.asp

http://www.informit.com/articles/article.aspx

Page 31: Handling error & exception in php

Presenter: Pravasini Sahoo, Mindfire Solutions

Page 32: Handling error & exception in php

https://www.facebook.com/pravasini.sahoo.9

http://in.linkedin.com/pub/pravasini-sahoo/22/a4a/80

https://twitter.com/pravasinisahoo

Presenter: Pravasini Sahoo, Mindfire SolutionsPresenter: Pravasini Sahoo, Mindfire Solutions

https://plus.google.com/116915409923931471434/