SlideShare a Scribd company logo
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 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.
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()
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
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
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");
}
Exception Handling in PHP
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
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:
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();
}
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.
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:
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
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 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.
__autoload() - Example
function __autoload($class)
{
// Require PEAR-compatible classes
require_once(“$class.php”)
}
$obj = new SomeClass();
__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
__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
__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
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, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com
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

More Related Content

What's hot (20)

PDF
Exceptions in PHP
JanTvrdik
 
PPT
Surprise! It's PHP :) (unabridged)
Sharon Levy
 
PPT
PHP - Introduction to PHP - Mazenet Solution
Mazenetsolution
 
ODP
Perl exceptions lightning talk
Peter Edwards
 
PPT
Php(report)
Yhannah
 
PDF
Methods of debugging - Atomate.net
Vitalie Chiperi
 
PPTX
Lesson 4 constant
MLG College of Learning, Inc
 
PPTX
Decision Structures
primeteacher32
 
PPT
Bioinformatica 27-10-2011-p4-files
Prof. Wim Van Criekinge
 
PPTX
Basic of PHP
Nisa Soomro
 
PPTX
Python Programming Essentials - M21 - Exception Handling
P3 InfoTech Solutions Pvt. Ltd.
 
PDF
Introduction to PHP - Basics of PHP
wahidullah mudaser
 
ODP
Debugging With Php
Automatem Ltd
 
DOC
Php tutorial
S Bharadwaj
 
PPTX
PHP 5.3
Chris Stone
 
PPTX
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
PPT
Introduction to PHP
Jussi Pohjolainen
 
PPT
Beginners PHP Tutorial
alexjones89
 
PPTX
Error and Exception Handling in PHP
Arafat Hossan
 
PPTX
Php introduction and configuration
Vijay Kumar Verma
 
Exceptions in PHP
JanTvrdik
 
Surprise! It's PHP :) (unabridged)
Sharon Levy
 
PHP - Introduction to PHP - Mazenet Solution
Mazenetsolution
 
Perl exceptions lightning talk
Peter Edwards
 
Php(report)
Yhannah
 
Methods of debugging - Atomate.net
Vitalie Chiperi
 
Lesson 4 constant
MLG College of Learning, Inc
 
Decision Structures
primeteacher32
 
Bioinformatica 27-10-2011-p4-files
Prof. Wim Van Criekinge
 
Basic of PHP
Nisa Soomro
 
Python Programming Essentials - M21 - Exception Handling
P3 InfoTech Solutions Pvt. Ltd.
 
Introduction to PHP - Basics of PHP
wahidullah mudaser
 
Debugging With Php
Automatem Ltd
 
Php tutorial
S Bharadwaj
 
PHP 5.3
Chris Stone
 
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
Introduction to PHP
Jussi Pohjolainen
 
Beginners PHP Tutorial
alexjones89
 
Error and Exception Handling in PHP
Arafat Hossan
 
Php introduction and configuration
Vijay Kumar Verma
 

Similar to Introduction to php exception and error management (20)

PDF
Object Oriented PHP - PART-2
Jalpesh Vasa
 
PDF
Module-4_WTA_PHP Class & Error Handling
SIVAKUMAR V
 
PPTX
object oriented programming in PHP & Functions
BackiyalakshmiVenkat
 
PPTX
lecture 15.pptx
ITNet
 
PDF
Sending emails through PHP
krishnapriya Tadepalli
 
PDF
Error Handling In PHP with all Try catch anf various runtime errors
PraveenHegde20
 
PPTX
Error handling
Meherul1234
 
PDF
Errors, Exceptions & Logging (PHPNW13 Uncon)
James Titcumb
 
PPTX
PHP 7 - A look at the future
Radu Murzea
 
PPTX
PHP7 - A look at the future
Radu Murzea
 
PPT
Php ppt
Sanmuga Nathan
 
PPT
Php Ppt
Hema Prasanth
 
PDF
Effective PHP. Part 6
Vasily Kartashov
 
ODP
PHP Basic
Yoeung Vibol
 
PDF
The why and how of moving to php 7
Wim Godden
 
PPT
Php5 vs php7
gentlex2
 
PPTX
Php debugging
Larry Ball
 
KEY
Let's creating your own PHP (tejimaya version)
Kousuke Ebihara
 
Object Oriented PHP - PART-2
Jalpesh Vasa
 
Module-4_WTA_PHP Class & Error Handling
SIVAKUMAR V
 
object oriented programming in PHP & Functions
BackiyalakshmiVenkat
 
lecture 15.pptx
ITNet
 
Sending emails through PHP
krishnapriya Tadepalli
 
Error Handling In PHP with all Try catch anf various runtime errors
PraveenHegde20
 
Error handling
Meherul1234
 
Errors, Exceptions & Logging (PHPNW13 Uncon)
James Titcumb
 
PHP 7 - A look at the future
Radu Murzea
 
PHP7 - A look at the future
Radu Murzea
 
Php Ppt
Hema Prasanth
 
Effective PHP. Part 6
Vasily Kartashov
 
PHP Basic
Yoeung Vibol
 
The why and how of moving to php 7
Wim Godden
 
Php5 vs php7
gentlex2
 
Php debugging
Larry Ball
 
Let's creating your own PHP (tejimaya version)
Kousuke Ebihara
 
Ad

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
PDF
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 3 stored procedures
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Ad

Recently uploaded (20)

PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
Q2 Leading a Tableau User Group - Onboarding
lward7
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
July Patch Tuesday
Ivanti
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Q2 Leading a Tableau User Group - Onboarding
lward7
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
July Patch Tuesday
Ivanti
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 

Introduction to php exception and error management

  • 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. 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. 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. 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. 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"); }
  • 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. 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. 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. 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. 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. 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
  • 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. __autoload() - Example function __autoload($class) { // Require PEAR-compatible classes require_once(“$class.php”) } $obj = new SomeClass();
  • 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. __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. __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. Questions? “A good question deserve a good grade…”
  • 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: [email protected]
  • 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