2. Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
6. EXCEPTION
Exception handling is used to change the normal flow of the code
execution if a specified error condition occurs. This condition is called an
exception.
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
7. Different error handling methods
•
•
•
•
•
Basic use of Exceptions
Creating a custom exception handler
Multiple exceptions
Re-throwing an exception
Setting a top level exception handler
8. Basic Use of Exceptions
When an exception is thrown, the code
following it will not be executed, and PHP
will try to find the matching "catch" block.
9. Creating a Custom Exception Class
• Creating a custom exception handler is quite
simple. We simply create a special class with
functions that can be called when an
exception occurs in PHP. The class must be an
extension of the exception class.
10. Multiple Exceptions
It is possible to use several if..else blocks, a
switch, or nest multiple exceptions. These
exceptions can use different exception classes
and return different error messages:
11. Re-throwing Exceptions
A script should hide system errors from users.
System errors may be important for the coder,
but is of no interest to the user. To make
things easier for the user you can re-throw the
exception with a user friendly message
12. Set a Top Level Exception Handler
• The set_exception_handler() function sets a
user-defined function to handle all uncaught
exceptions
13. Try, throw and catch
Try - A function using an exception should be in a "try" block. If the exception
does not trigger, the code will continue as normal. However if the
exception triggers, an exception is "thrown".
Throw - This is how you trigger an exception. Each "throw" must have at least
one "catch".
Catch - - A "catch" block retrieves an exception and creates an object
containing the exception information.
14. The Basic Exception Class
exceptions are objects that must be direct or indirect instances of the Exception base class.
For eg:
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();
18. 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
19. Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550