introduction to php exception and error management

23
Error Management in PHP

Upload: baabtracom-no-1-supplier-of-quality-freshers

Post on 18-Jun-2015

349 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Introduction to php  exception and error management

Error Management in PHP

Page 2: Introduction to php  exception and error management

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.

Page 3: Introduction to php  exception and error management

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()

Page 4: Introduction to php  exception and error management

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

Page 5: Introduction to php  exception and error management

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

Page 6: Introduction to php  exception and error management

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 functionfunction 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 handlerset_error_handler("customError");

$test=2;

//trigger errorif ($test>1)

{trigger_error("A custom error has been

triggered");}

?>

Page 7: Introduction to php  exception and error management

Exception Handling in PHP

Page 8: Introduction to php  exception and error management

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

Page 9: Introduction to php  exception and error management

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:

Page 10: Introduction to php  exception and error management

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 codefinal function getCode();// Returns the file namefinal function getFile();// Returns the file linefinal function getLine();// Returns an execution backtrace as an arrayfinal function getTrace();// Returns a backtrace as a stringfinal function getTraceAsString();// Returns a string representation of the exceptionfunction __toString();

}

Page 11: Introduction to php  exception and error management

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.

Page 12: Introduction to php  exception and error management

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:

Page 13: Introduction to php  exception and error management

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

Page 14: Introduction to php  exception and error management

Lazy Loading

Page 15: Introduction to php  exception and error management

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.

Page 16: Introduction to php  exception and error management

__autoload() - Example

function __autoload($class)

{

// Require PEAR-compatible classes

require_once(“$class.php”)

}

$obj = new SomeClass();

Page 17: Introduction to php  exception and error management

__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

Page 18: Introduction to php  exception and error management

__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

Page 19: Introduction to php  exception and error management

__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

Page 20: Introduction to php  exception and error management

Questions?

“A good question deserve a good grade…”

Page 21: Introduction to php  exception and error management

End of day

Page 22: Introduction to php  exception and error management

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]

Page 23: Introduction to php  exception and error management

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