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
PPT
Error reporting in php
PPTX
lecture 15.pptx
PPTX
Error handling
PPTX
Error and Exception Handling in PHP
PDF
Object Oriented PHP - PART-2
PDF
Sending emails through PHP
PPT
Php Error Handling
PHP - Introduction to PHP Error Handling
Error reporting in php
lecture 15.pptx
Error handling
Error and Exception Handling in PHP
Object Oriented PHP - PART-2
Sending emails through PHP
Php Error Handling

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

PDF
Elegant Ways of Handling PHP Errors and Exceptions
PDF
Introduction to php exception and error management
PDF
Module-4_WTA_PHP Class & Error Handling
PDF
Errors, Exceptions & Logging (PHP Hants Oct '13)
PDF
Errors, Exceptions & Logging (PHPNW13 Uncon)
PPTX
object oriented programming in PHP & Functions
ODP
PHP Basic
PPTX
Errors
PDF
Types of Error in PHP
PPTX
Php debugging
PPT
Php Ppt
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PPTX
PHP Basics
PPTX
Handling error & exception in php
DOC
basic error handling wesite
DOCX
Php advance
PPTX
Unit-1 PHP Basic1 of the understanding of php.pptx
PPSX
DIWE - Fundamentals of PHP
PDF
Even better debugging; Equipped yourself with powerful tools.
Elegant Ways of Handling PHP Errors and Exceptions
Introduction to php exception and error management
Module-4_WTA_PHP Class & Error Handling
Errors, Exceptions & Logging (PHP Hants Oct '13)
Errors, Exceptions & Logging (PHPNW13 Uncon)
object oriented programming in PHP & Functions
PHP Basic
Errors
Types of Error in PHP
Php debugging
Php Ppt
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP Basics
Handling error & exception in php
basic error handling wesite
Php advance
Unit-1 PHP Basic1 of the understanding of php.pptx
DIWE - Fundamentals of PHP
Even better debugging; Equipped yourself with powerful tools.
Ad

More from PraveenHegde20 (6)

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

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PPTX
Institutional Correction lecture only . . .
PDF
Classroom Observation Tools for Teachers
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Presentation on HIE in infants and its manifestations
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Complications of Minimal Access Surgery at WLH
Cell Structure & Organelles in detailed.
Institutional Correction lecture only . . .
Classroom Observation Tools for Teachers
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
O7-L3 Supply Chain Operations - ICLT Program
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Computing-Curriculum for Schools in Ghana
Abdominal Access Techniques with Prof. Dr. R K Mishra
Final Presentation General Medicine 03-08-2024.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Presentation on HIE in infants and its manifestations
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
O5-L3 Freight Transport Ops (International) V1.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Supply Chain Operations Speaking Notes -ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Complications of Minimal Access Surgery at WLH

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