SlideShare a Scribd company logo
Error Handling In PHP with all Try catch anf various runtime errors
Introduction to PHP
Introduction to PHP
Error Handling
Error Handling
Types
Types
There are 12 unique error types, which can
be grouped into 3 main categories:
• Informational (Notices)
• Actionable (Warnings)
• Fatal
Informational Errors
Informational Errors
• Harmless problem, and can be avoided through
use of explicit programming.
e.g. use of an undefined variable, defining a string
without quotes, etc.
Actionable Errors
Actionable Errors
• Indicate that something clearly wrong has
happened and that action should be taken.
e.g. file not present, database not available,
missing function arguments, etc.
Fatal Errors
Fatal Errors
• Something so terrible has happened during
execution of your script that further
processing simply cannot continue.
e.g. parsing error, calling an undefined
function, etc.
Identifying Errors
Identifying Errors
E_STRICT Feature or behaviour is depreciated (PHP5).
E_NOTICE Detection of a situation that could indicate a problem,
but might be normal.
E_USER_NOTICE User triggered notice.
E_WARNING Actionable error occurred during execution.
E_USER_WARNING User triggered warning.
E_COMPILE_WARNING Error occurred during script compilation (unusual)
E_CORE_WARNING Error during initialization of the PHP engine.
E_ERROR Unrecoverable error in PHP code execution.
E_USER_ERROR User triggered fatal error.
E_COMPILE_ERROR Critical error occurred while trying to read script.
E_CORE_ERROR Occurs if PHP engine cannot startup/etc.
E_PARSE Raised during compilation in response to syntax error
notice
warning
fatal
Causing errors
Causing errors
• It is possible to cause PHP at any point in your
script.
trigger_error($msg,$type);
e.g.
…
if (!$db_conn) {
trigger_error(‘db conn
failed’,E_USER_ERROR);
}
…
PHP Error Handling
PHP Error Handling
Customizing Error
Customizing Error
Handling
Handling
• Generally, how PHP handles errors is defined
by various constants in the installation (php.ini).
• There are several things you can control in
your scripts however..
Set error reporting
Set error reporting
settings
settings
error_reporting($level)
This function can be used to control which
errors are displayed, and which are simply
ignored. The effect only lasts for the duration
of the execution of your script.
Set error reporting settings
Set error reporting settings
<?php
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
error_reporting(E_ALL ^ E_NOTICE);
// Report ALL PHP errors
error_reporting(E_ALL);
?>
Set error reporting
Set error reporting
settings
settings
• Hiding errors is NOT a solution to a problem.
• It is useful, however, to hide any errors
produced on a live server.
• While developing and debugging code,
displaying all errors is highly recommended!
Suppressing Errors
Suppressing Errors
• The special @ operator can be used to
suppress function errors.
• Any error produced by the function is
suppressed and not displayed by PHP
regardless of the error reporting setting.
Suppressing Errors
Suppressing Errors
$db = @mysql_connect($h,$u,
$p);
if (!$db) {
trigger_error(‘blah’,E_USER_
ERROR);
}
Suppressing Errors
Suppressing Errors
$db = @mysql_connect($h,$u,$p);
if (!$db) {
trigger_error(blah.',E_USER_ERROR
);
}
$db = @mysql_connect($h,$u,$p);
Attempt to connect to
database. Suppress error
notice if it fails..
Suppressing Errors
Suppressing Errors
$db = @mysql_connect($h,$u,$p);
if (!$db) {
trigger_error(‘blah’,E_USER_ERROR);
}
Since error is suppressed, it
must be handled gracefully
somewhere else..
Suppressing Errors
Suppressing Errors
• Error suppression is NOT a solution to a
problem.
• It can be useful to locally define your own
error handling mechanisms.
• If you suppress any errors, you must check
for them yourself elsewhere.
Custom Error Handler
Custom Error Handler
• You can write your own function to handle
PHP errors in any way you want.
• You simply need to write a function with
appropriate inputs, then register it in your
script as the error handler.
• The handler function should be able to
receive 4 arguments, and return true to
indicate it has handled the error…
Custom Error Handler
Custom Error Handler
function err_handler(
$errcode,$errmsg,$file,$lineno) {
echo ‘An error has occurred!<br />’;
echo “file: $file<br />”;
echo “line: $lineno<br />”;
echo “Problem: $errmsg”;
return true;
}
Custom Error Handler
Custom Error Handler
function err_handler(
$errcode,$errmsg,$file,$lineno) {
echo ‘An error has occurred!<br />’;
echo “file: $file<br />”;
echo “line: $lineno<br />”;
echo “Problem: $errmsg”;
return true;
}
$errcode,$errmsg,$file,$lineno) {
The handler must have 4 inputs..
1.error code
2.error message
3.file where error occurred
4.line at which error occurred
Custom Error Handler
Custom Error Handler
function err_handler(
$errcode,$errmsg,$file,$lineno) {
echo ‘An error has occurred!<br />’;
echo “file: $file<br />”;
echo “line: $lineno<br />”;
echo “Problem: $errmsg”;
return true;
}
echo ‘An error has occurred!<br />’;
echo “file: $file<br />”;
echo “line: $lineno<br />”;
echo “Problem: $errmsg”;
Any PHP statements can be
executed…
Custom Error Handler
Custom Error Handler
function err_handler(
$errcode,$errmsg,$file,$lineno) {
echo ‘An error has occurred!<br />’;
echo “file: $file<br />”;
echo “line: $lineno<br />”;
echo “Problem: $errmsg”;
return true;
}
return true;
Return true to let PHP know
that the custom error handler
has handled the error OK.
Custom Error Handler
Custom Error Handler
• The function then needs to be registered as your
custom error handler:
set_error_handler(‘err_handler’);
• You can ‘mask’ the custom error handler so it
only receives certain types of error. e.g. to
register a custom handler just for user triggered
errors:
set_error_handler(‘err_handler’,
E_USER_NOTICE | E_USER_WARNING | E_USER_ERROR);
Custom Error Handler
Custom Error Handler
• A custom error handler is never passed
E_PARSE, E_CORE_ERROR or
E_COMPILE_ERROR errors as these are
considered too dangerous.
• Often used in conjunction with a ‘debug’
flag for neat combination of debug and
production code display..
Thank
Thank You !!!
You !!!
For More Information click below link:
Follow Us on:
http://vibranttechnologies.co.in/php-classes-in-mumbai.html

More Related Content

PPT
PHP - Introduction to PHP Error Handling
Vibrant Technologies & Computers
 
PPT
Error reporting in php
Mudasir Syed
 
PPTX
lecture 15.pptx
ITNet
 
PPTX
Error handling
Meherul1234
 
PPTX
Error and Exception Handling in PHP
Arafat Hossan
 
PDF
Object Oriented PHP - PART-2
Jalpesh Vasa
 
PDF
Sending emails through PHP
krishnapriya Tadepalli
 
PPT
Php Error Handling
mussawir20
 
PHP - Introduction to PHP Error Handling
Vibrant Technologies & Computers
 
Error reporting in php
Mudasir Syed
 
lecture 15.pptx
ITNet
 
Error handling
Meherul1234
 
Error and Exception Handling in PHP
Arafat Hossan
 
Object Oriented PHP - PART-2
Jalpesh Vasa
 
Sending emails through PHP
krishnapriya Tadepalli
 
Php Error Handling
mussawir20
 

Similar to Error Handling In PHP with all Try catch anf various runtime errors (20)

PDF
Elegant Ways of Handling PHP Errors and Exceptions
ZendCon
 
PDF
Introduction to php exception and error management
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Module-4_WTA_PHP Class & Error Handling
SIVAKUMAR V
 
PDF
Errors, Exceptions & Logging (PHP Hants Oct '13)
James Titcumb
 
PDF
Errors, Exceptions & Logging (PHPNW13 Uncon)
James Titcumb
 
PPTX
object oriented programming in PHP & Functions
BackiyalakshmiVenkat
 
ODP
PHP Basic
Yoeung Vibol
 
PPTX
Errors
agjmills
 
PDF
Types of Error in PHP
Vineet Kumar Saini
 
PPTX
Php debugging
Larry Ball
 
PPT
Php Ppt
Hema Prasanth
 
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
PPTX
PHP Basics
Bhaktaraz Bhatta
 
PPTX
Handling error & exception in php
Pravasini Sahoo
 
DOC
basic error handling wesite
PutuMahendra Wijaya
 
DOCX
Php advance
Rattanjeet Singh
 
PPTX
Unit-1 PHP Basic1 of the understanding of php.pptx
AatifKhan84
 
PPSX
DIWE - Fundamentals of PHP
Rasan Samarasinghe
 
PDF
Even better debugging; Equipped yourself with powerful tools.
Murshed Ahmmad Khan
 
Elegant Ways of Handling PHP Errors and Exceptions
ZendCon
 
Introduction to php exception and error management
baabtra.com - No. 1 supplier of quality freshers
 
Module-4_WTA_PHP Class & Error Handling
SIVAKUMAR V
 
Errors, Exceptions & Logging (PHP Hants Oct '13)
James Titcumb
 
Errors, Exceptions & Logging (PHPNW13 Uncon)
James Titcumb
 
object oriented programming in PHP & Functions
BackiyalakshmiVenkat
 
PHP Basic
Yoeung Vibol
 
Errors
agjmills
 
Types of Error in PHP
Vineet Kumar Saini
 
Php debugging
Larry Ball
 
Php Ppt
Hema Prasanth
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
PHP Basics
Bhaktaraz Bhatta
 
Handling error & exception in php
Pravasini Sahoo
 
basic error handling wesite
PutuMahendra Wijaya
 
Php advance
Rattanjeet Singh
 
Unit-1 PHP Basic1 of the understanding of php.pptx
AatifKhan84
 
DIWE - Fundamentals of PHP
Rasan Samarasinghe
 
Even better debugging; Equipped yourself with powerful tools.
Murshed Ahmmad Khan
 
Ad

More from PraveenHegde20 (6)

PPTX
What-is-Laravel and introduciton to Laravel
PraveenHegde20
 
PPT
PHP with Postgres SQL connection string and connecting
PraveenHegde20
 
PPT
CSS Adnaved with HTML abd complete Stylesheet
PraveenHegde20
 
PPT
CSS Basics ro advanced training material
PraveenHegde20
 
PDF
Smart city engineering work using Internet of Things
PraveenHegde20
 
PDF
Data Engineering Data warehousing Pentaho
PraveenHegde20
 
What-is-Laravel and introduciton to Laravel
PraveenHegde20
 
PHP with Postgres SQL connection string and connecting
PraveenHegde20
 
CSS Adnaved with HTML abd complete Stylesheet
PraveenHegde20
 
CSS Basics ro advanced training material
PraveenHegde20
 
Smart city engineering work using Internet of Things
PraveenHegde20
 
Data Engineering Data warehousing Pentaho
PraveenHegde20
 
Ad

Recently uploaded (20)

PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Landforms and landscapes data surprise preview
jpinnuck
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 

Error Handling In PHP with all Try catch anf various runtime errors

  • 2. Introduction to PHP Introduction to PHP Error Handling Error Handling
  • 3. Types Types There are 12 unique error types, which can be grouped into 3 main categories: • Informational (Notices) • Actionable (Warnings) • Fatal
  • 4. Informational Errors Informational Errors • Harmless problem, and can be avoided through use of explicit programming. e.g. use of an undefined variable, defining a string without quotes, etc.
  • 5. Actionable Errors Actionable Errors • Indicate that something clearly wrong has happened and that action should be taken. e.g. file not present, database not available, missing function arguments, etc.
  • 6. Fatal Errors Fatal Errors • Something so terrible has happened during execution of your script that further processing simply cannot continue. e.g. parsing error, calling an undefined function, etc.
  • 7. Identifying Errors Identifying Errors E_STRICT Feature or behaviour is depreciated (PHP5). E_NOTICE Detection of a situation that could indicate a problem, but might be normal. E_USER_NOTICE User triggered notice. E_WARNING Actionable error occurred during execution. E_USER_WARNING User triggered warning. E_COMPILE_WARNING Error occurred during script compilation (unusual) E_CORE_WARNING Error during initialization of the PHP engine. E_ERROR Unrecoverable error in PHP code execution. E_USER_ERROR User triggered fatal error. E_COMPILE_ERROR Critical error occurred while trying to read script. E_CORE_ERROR Occurs if PHP engine cannot startup/etc. E_PARSE Raised during compilation in response to syntax error notice warning fatal
  • 8. Causing errors Causing errors • It is possible to cause PHP at any point in your script. trigger_error($msg,$type); e.g. … if (!$db_conn) { trigger_error(‘db conn failed’,E_USER_ERROR); } …
  • 9. PHP Error Handling PHP Error Handling
  • 10. Customizing Error Customizing Error Handling Handling • Generally, how PHP handles errors is defined by various constants in the installation (php.ini). • There are several things you can control in your scripts however..
  • 11. Set error reporting Set error reporting settings settings error_reporting($level) This function can be used to control which errors are displayed, and which are simply ignored. The effect only lasts for the duration of the execution of your script.
  • 12. Set error reporting settings Set error reporting settings <?php // Turn off all error reporting error_reporting(0); // Report simple running errors error_reporting(E_ERROR | E_WARNING | E_PARSE); // Reporting E_NOTICE can be good too (to report uninitialized // variables or catch variable name misspellings ...) error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); // Report all errors except E_NOTICE error_reporting(E_ALL ^ E_NOTICE); // Report ALL PHP errors error_reporting(E_ALL); ?>
  • 13. Set error reporting Set error reporting settings settings • Hiding errors is NOT a solution to a problem. • It is useful, however, to hide any errors produced on a live server. • While developing and debugging code, displaying all errors is highly recommended!
  • 14. Suppressing Errors Suppressing Errors • The special @ operator can be used to suppress function errors. • Any error produced by the function is suppressed and not displayed by PHP regardless of the error reporting setting.
  • 15. Suppressing Errors Suppressing Errors $db = @mysql_connect($h,$u, $p); if (!$db) { trigger_error(‘blah’,E_USER_ ERROR); }
  • 16. Suppressing Errors Suppressing Errors $db = @mysql_connect($h,$u,$p); if (!$db) { trigger_error(blah.',E_USER_ERROR ); } $db = @mysql_connect($h,$u,$p); Attempt to connect to database. Suppress error notice if it fails..
  • 17. Suppressing Errors Suppressing Errors $db = @mysql_connect($h,$u,$p); if (!$db) { trigger_error(‘blah’,E_USER_ERROR); } Since error is suppressed, it must be handled gracefully somewhere else..
  • 18. Suppressing Errors Suppressing Errors • Error suppression is NOT a solution to a problem. • It can be useful to locally define your own error handling mechanisms. • If you suppress any errors, you must check for them yourself elsewhere.
  • 19. Custom Error Handler Custom Error Handler • You can write your own function to handle PHP errors in any way you want. • You simply need to write a function with appropriate inputs, then register it in your script as the error handler. • The handler function should be able to receive 4 arguments, and return true to indicate it has handled the error…
  • 20. Custom Error Handler Custom Error Handler function err_handler( $errcode,$errmsg,$file,$lineno) { echo ‘An error has occurred!<br />’; echo “file: $file<br />”; echo “line: $lineno<br />”; echo “Problem: $errmsg”; return true; }
  • 21. Custom Error Handler Custom Error Handler function err_handler( $errcode,$errmsg,$file,$lineno) { echo ‘An error has occurred!<br />’; echo “file: $file<br />”; echo “line: $lineno<br />”; echo “Problem: $errmsg”; return true; } $errcode,$errmsg,$file,$lineno) { The handler must have 4 inputs.. 1.error code 2.error message 3.file where error occurred 4.line at which error occurred
  • 22. Custom Error Handler Custom Error Handler function err_handler( $errcode,$errmsg,$file,$lineno) { echo ‘An error has occurred!<br />’; echo “file: $file<br />”; echo “line: $lineno<br />”; echo “Problem: $errmsg”; return true; } echo ‘An error has occurred!<br />’; echo “file: $file<br />”; echo “line: $lineno<br />”; echo “Problem: $errmsg”; Any PHP statements can be executed…
  • 23. Custom Error Handler Custom Error Handler function err_handler( $errcode,$errmsg,$file,$lineno) { echo ‘An error has occurred!<br />’; echo “file: $file<br />”; echo “line: $lineno<br />”; echo “Problem: $errmsg”; return true; } return true; Return true to let PHP know that the custom error handler has handled the error OK.
  • 24. Custom Error Handler Custom Error Handler • The function then needs to be registered as your custom error handler: set_error_handler(‘err_handler’); • You can ‘mask’ the custom error handler so it only receives certain types of error. e.g. to register a custom handler just for user triggered errors: set_error_handler(‘err_handler’, E_USER_NOTICE | E_USER_WARNING | E_USER_ERROR);
  • 25. Custom Error Handler Custom Error Handler • A custom error handler is never passed E_PARSE, E_CORE_ERROR or E_COMPILE_ERROR errors as these are considered too dangerous. • Often used in conjunction with a ‘debug’ flag for neat combination of debug and production code display..
  • 26. Thank Thank You !!! You !!! For More Information click below link: Follow Us on: http://vibranttechnologies.co.in/php-classes-in-mumbai.html