Your SlideShare is downloading. ×
0
Error Management in PHP
Types of errors
Compile-time errors Errors detected by the parser while it is compiling
a script. Cannot be trapped from w...
Error Reporting
• By default, PHP reports any errors it encounters to the script’s
output unless you control it as follows...
1. Configuring Php.ini
– Several configuration directives in the php.ini file allow you to fine tune
how—and which—errors ...
1. Configuring Php.ini
• error_reporting
– determines which errors are reported by PHP
– Eg: error_reporting=E_ALL & ~E_NO...
2. Handling Errors
• The set_error_handler() function sets a user-defined function to
handle errors.
• Eg: set_error_handl...
Exception Handling in PHP
Exception Handling in PHP
• Exceptions provide an error control mechanism that is more fine-
grained than traditional PHP ...
The Basic Exception Class
• As we mentioned in the previous paragraph, exceptions are objects
that must be direct or indir...
The Basic Exception Class
class Exception {
// The error message associated with this exception
protected $message = ’Unkn...
The Basic Exception Class
• Almost all of the properties of an Exception are automatically
filled in for you by the interp...
Throwing Exceptions
• Exceptions are usually created and thrown when an error occurs by using
the throw construct:
if ($er...
Example
try {
if ($error) {
throw new Exception ("This is my error");
}
} catch (Exception $e) {
echo $e->getMessage();
}
...
Lazy Loading
Lazy Loading
• Prior to PHP 5, if you try to create an object on an undefined class(perhaps
that class is written on anoth...
__autoload() - Example
function __autoload($class)
{
// Require PEAR-compatible classes
require_once(“$class.php”)
}
$obj ...
__autoload() - Example
function __autoload($class)
{
// Require PEAR-compatible classes
require_once(“$class.php”)
}
$obj ...
__autoload() - Example
function __autoload($class)
{
// Require PEAR-compatible classes
require_once(“$class.php”)
}
$obj ...
__autoload() - Example
function __autoload($class)
{
// Require PEAR-compatible classes
require_once(“$class.php”)
}
$obj ...
Questions?
“A good question deserve a good grade…”
End of day
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, N...
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.co...
Upcoming SlideShare
Loading in...5
×

Introduction to php exception and error management

355

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
355
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Introduction to php exception and error management"

  1. 1. Error Management in PHP
  2. 2. Types of errors Compile-time errors Errors detected by the parser while it is compiling a script. Cannot be trapped from within the script itself. Fatal errors Errors that halt the execution of a script. Cannot be trapped. Recoverable errors Errors that represent significant failures, but can still be handled in a safe way. Warnings Recoverable errors that indicate a run-time fault. Do not halt the execution of the script. Notices Indicate that an error condition occurred, but is not necessarily significant. Do not halt the execution of the script.
  3. 3. Error Reporting • By default, PHP reports any errors it encounters to the script’s output unless you control it as follows 1. Configuring Php.ini 2. Handling Errors by setting set_error_handler()
  4. 4. 1. Configuring Php.ini – Several configuration directives in the php.ini file allow you to fine tune how—and which—errors are reported. – error_reporting, – display_errors – log_errors. You can find these in php.ini file
  5. 5. 1. Configuring Php.ini • error_reporting – determines which errors are reported by PHP – Eg: error_reporting=E_ALL & ~E_NOTICE • display_errors – if turned on, errors are outputted to the script’s output; this is not desirable in a production environment, as everyone will be able to see your scripts’ errors. Eg: display_errors = ON • log_errors, – which causes errors to be written to your web server’s error log, so that only developers can utilise it and end users will not be informed of same – Eg:log_errors,=ON
  6. 6. 2. Handling Errors • The set_error_handler() function sets a user-defined function to handle errors. • Eg: set_error_handler(error_function,error_types) <?php //error handler function function customError($errno, $errstr, $errfile, $errline) { echo "<b>Custom error:</b> [$errno] $errstr<br />"; echo " Error on line $errline in $errfile<br />"; echo "Ending Script"; die(); } //set error handler set_error_handler("customError"); $test=2; //trigger error if ($test>1) { trigger_error("A custom error has been triggered"); }
  7. 7. Exception Handling in PHP
  8. 8. Exception Handling in PHP • Exceptions provide an error control mechanism that is more fine- grained than traditional PHP fault handling.There are several key differences between “regular” PHP errors and exceptions: • Exceptions are objects, created (or “thrown”) when an error occurs • Exceptions can be handled at different points in a script’s execution, and different types of exceptions can be handled by separate portions of a script’s code • All unhandled exceptions are fatal • Exceptions can be thrown from the __construct method on failure • Exceptions change the flow of the application
  9. 9. The Basic Exception Class • As we mentioned in the previous paragraph, exceptions are objects that must be direct or indirect (for example through inheritance) instances of the Exception base class. • The latter is built into PHP itself, and is declared as follows:
  10. 10. The Basic Exception Class class Exception { // The error message associated with this exception protected $message = ’Unknown Exception’; // The error code associated with this exception protected $code = 0; // The pathname of the file where the exception occurred protected $file; // The line of the file where the exception occurred protected $line; // Constructor function __construct ($message = null, $code = 0); // Returns the message final function getMessage(); // Returns the error code final function getCode(); // Returns the file name final function getFile(); // Returns the file line final function getLine(); // Returns an execution backtrace as an array final function getTrace(); // Returns a backtrace as a string final function getTraceAsString(); // Returns a string representation of the exception function __toString(); }
  11. 11. The Basic Exception Class • Almost all of the properties of an Exception are automatically filled in for you by the interpreter—generally speaking, you only need to provide a message and a code, and all the remaining information will be taken care of for you. • Since Exception is a normal (if built-in) class, you can extend it and effectively create your own exceptions, thus providing your error handlers with any additional information that your application requires.
  12. 12. Throwing Exceptions • Exceptions are usually created and thrown when an error occurs by using the throw construct: if ($error) { throw new Exception ("This is my error"); } • Exceptions then “bubble up” until they are either handled by the script or cause a fatal exception • The handling of exceptions is performed using a try...catch block:
  13. 13. Example try { if ($error) { throw new Exception ("This is my error"); } } catch (Exception $e) { echo $e->getMessage(); } • In the example above, any exception that is thrown inside the try{} block is going to be caught and passed on the code inside the catch{} block, where it can be handled
  14. 14. Lazy Loading
  15. 15. Lazy Loading • Prior to PHP 5, if you try to create an object on an undefined class(perhaps that class is written on another file which you forgot to include) would cause a fatal error. • This meant that you needed to include all of the class files that you might need, rather than loading them as they were needed • To solve this problem, PHP 5 features an “autoload” facility that makes it possible to implement “lazy loading”, or loading of classes on-demand only • When we try to create an object of undefined class PHP will try to call the __autoload() global magic function so that the script may be given an opportunity to load it.
  16. 16. __autoload() - Example function __autoload($class) { // Require PEAR-compatible classes require_once(“$class.php”) } $obj = new SomeClass();
  17. 17. __autoload() - Example function __autoload($class) { // Require PEAR-compatible classes require_once(“$class.php”) } $obj = new SomeClass(); You tried to create an object of SomeClass. But you forgot that someClass is defined in another page called SomeClass.php
  18. 18. __autoload() - Example function __autoload($class) { // Require PEAR-compatible classes require_once(“$class.php”) } $obj = new SomeClass(); When you tried to do so, PHP will automatically calls __autoload() which will have an argument ie the class name for which object we tried to create
  19. 19. __autoload() - Example function __autoload($class) { // Require PEAR-compatible classes require_once(“$class.php”) } $obj = new SomeClass(); We are including the file called “someClass.php” The advantage of lazy loading is that we can include files upon its requirement only rather than including all the files unnecessarily
  20. 20. Questions? “A good question deserve a good grade…”
  21. 21. End of day
  22. 22. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com
  23. 23. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×