Topic: Error and Exception Handling in PHP
Error and Exception Handling in PHP
Contents
• Error and Error Handling
• Using die() function
• Defining Custom Error Handling Function
• Error Parameter
• Possible Error levels
• Possible Error levels Exceptions Handling
• Creating Custom Exception Handler
Page 2 out of 10Error and Exception Handling in PHP
Error and Error Handling
• An error is something you have done which is considered to be incorrect or
wrong, or which should not have been done.
• Error handling is the process of catching errors raised by our program and then
taking appropriate action to prevent unforeseen consequences.
Page 3 out of 10Error and Exception Handling in PHP
Using die() function
By using die() function we can stop the process or jump to the next process if there
is a error. Normally it is used when file is not found.
Example:
Page 4 out of 10
if(!file_exists("/tmp/test.txt")) {
die("File not found"); }
else
{ $file = fopen("/tmp/test.txt","r");
print "Opend file; }
Error and Exception Handling in PHP
Defining Custom Error Handling Function
• We can write our own function to handling any error. PHP provides us a
framework to define error handling function.
• This function must be able to handle minimum two parameters (error level and
error message) but can accept up to five parameters (optionally: file, line-
number, and the error context)
Syntax:
Error_Function_Name(Error_Level, Error_Message, Error_File, Error_Line, Error_Context)
Page 5 out of 10Error and Exception Handling in PHP
Error Parameter
There are 5 kinds of error parameters
• Error_level - A value number, specifies the error report level for the user-defined
error.
• Error_message - Message for the user-defined error
• Error_file - File name in which the error occurred
• Error_line - Line number in which the error occurred
• Error_context - Array containing every variable and their values in use when the
error occurred.
Page 6 out of 10Error and Exception Handling in PHP
Possible Error levels
• .E_ERROR - Fatal run-time errors
• E_WARNING - Non-fatal run-time errors
• E_PARSE - Compile-time parse errors
• E_NOTICE - Run-time notices.
• E_CORE_ERROR - Fatal errors that occur during PHP's initial start-up.
• E_CORE_WARNING - Non-fatal run-time errors. This occurs during PHP's initial
start-up.
• E_USER_ERROR - Fatal user-generated error.
• E_USER_WARNING - Non-fatal user-generated warning.
• E_USER_NOTICE - User-generated notice.
• E_ALL - All errors and warnings, except level E_STRICT
Page 7 out of 10Error and Exception Handling in PHP
Exceptions Handling
PHP 5 has an exception model similar to that of other programming languages.
Exceptions are important and provides a better control over error handling.
• 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.
Page 8 out of 10Error and Exception Handling in PHP
Creating Custom Exception Handler
We can define our own custom exception handler. By using following function to
set a user-defined exception handler function.
Syntax:
string set_exception_handler ( callback $exception_handler )
Example:
<?php
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "n";
}
set_exception_handler('exception_handler');
throw new Exception('Uncaught Exception');
echo "Not Executedn";
?>
Page 9 out of 10Error and Exception Handling in PHP
Page 10 out of 10Error and Exception Handling in PHP

Error and Exception Handling in PHP

  • 1.
    Topic: Error andException Handling in PHP Error and Exception Handling in PHP
  • 2.
    Contents • Error andError Handling • Using die() function • Defining Custom Error Handling Function • Error Parameter • Possible Error levels • Possible Error levels Exceptions Handling • Creating Custom Exception Handler Page 2 out of 10Error and Exception Handling in PHP
  • 3.
    Error and ErrorHandling • An error is something you have done which is considered to be incorrect or wrong, or which should not have been done. • Error handling is the process of catching errors raised by our program and then taking appropriate action to prevent unforeseen consequences. Page 3 out of 10Error and Exception Handling in PHP
  • 4.
    Using die() function Byusing die() function we can stop the process or jump to the next process if there is a error. Normally it is used when file is not found. Example: Page 4 out of 10 if(!file_exists("/tmp/test.txt")) { die("File not found"); } else { $file = fopen("/tmp/test.txt","r"); print "Opend file; } Error and Exception Handling in PHP
  • 5.
    Defining Custom ErrorHandling Function • We can write our own function to handling any error. PHP provides us a framework to define error handling function. • This function must be able to handle minimum two parameters (error level and error message) but can accept up to five parameters (optionally: file, line- number, and the error context) Syntax: Error_Function_Name(Error_Level, Error_Message, Error_File, Error_Line, Error_Context) Page 5 out of 10Error and Exception Handling in PHP
  • 6.
    Error Parameter There are5 kinds of error parameters • Error_level - A value number, specifies the error report level for the user-defined error. • Error_message - Message for the user-defined error • Error_file - File name in which the error occurred • Error_line - Line number in which the error occurred • Error_context - Array containing every variable and their values in use when the error occurred. Page 6 out of 10Error and Exception Handling in PHP
  • 7.
    Possible Error levels •.E_ERROR - Fatal run-time errors • E_WARNING - Non-fatal run-time errors • E_PARSE - Compile-time parse errors • E_NOTICE - Run-time notices. • E_CORE_ERROR - Fatal errors that occur during PHP's initial start-up. • E_CORE_WARNING - Non-fatal run-time errors. This occurs during PHP's initial start-up. • E_USER_ERROR - Fatal user-generated error. • E_USER_WARNING - Non-fatal user-generated warning. • E_USER_NOTICE - User-generated notice. • E_ALL - All errors and warnings, except level E_STRICT Page 7 out of 10Error and Exception Handling in PHP
  • 8.
    Exceptions Handling PHP 5has an exception model similar to that of other programming languages. Exceptions are important and provides a better control over error handling. • 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. Page 8 out of 10Error and Exception Handling in PHP
  • 9.
    Creating Custom ExceptionHandler We can define our own custom exception handler. By using following function to set a user-defined exception handler function. Syntax: string set_exception_handler ( callback $exception_handler ) Example: <?php function exception_handler($exception) { echo "Uncaught exception: " , $exception->getMessage(), "n"; } set_exception_handler('exception_handler'); throw new Exception('Uncaught Exception'); echo "Not Executedn"; ?> Page 9 out of 10Error and Exception Handling in PHP
  • 10.
    Page 10 outof 10Error and Exception Handling in PHP