Error Handling in PHP
Jussi Pohjolainen
PHP Error Handling
- PHP has three major levels of errors:
E_NOTICE
E_WARNING
E_ERROR
E_NOTICE
E_NOTICE: you may do something that you don't want to do.
- Well implemented app doesn't have any
E_NOTICE - "errors".
- Example:
$var++; // $var is not initialized
E_NOTICE is off by default.
E_WARNING, E_ERROR and others
E_WARNING: nonfatal runtime error.
E_ERROR: unrecoverable error.
- Other errors include:
E_PARSE
E_COMPILE_ERROR
E_COMPILE_WARNING
E_CORE_ERROR
E_CORE_WARNING
Setting Error Reporting Levels
- Modify
php.ini file:
error_reporting = E_ALL
error_reporting = E_ALL ~ E_NOTICE // Default
- Or set in script:
error_reporting
Handling errors
- You can display, log, ignore or act on errors.
- Display:
php.ini: display_errors = On;
- Log:
php.ini: log_error = On; error_log = /path/to/filename
- Ignore:
$fp = @fopen(...)
- Acting on errors: Custom error handler:
set_error_handler(..)
Method errors
- What should method do, when an error occures?
- Return false? Return null? Return -1? Return string?
- How do you know when method return value is valid and when
return value is an error?
- Very confusing.
Exceptions
- Before PHP 5 exception handling was not possible
- PHP5 exception handling is very similar to Java
- Mechanism that is designed to handle errors
Exceptions
- In PHP5, exception is an object
- PHP5 has built-in
Exception-class that is designed specifically for
exceptions
- Creating exception: create object from exception class and throw it.
Example: throwing exception
function divide ($a, $b)
{
if( ! (is_int($a) and is_int($b)) )
{
throw new Exception();
}
if( $b === 0 )
{
throw new Exception();
}
return ( $a / $b );
}
Example: catching exception
try
{
$divide = divide( 3, 3 );
}
catch ( Exception $e )
{
print "Problem";
}
Creating your own Exception-classes
class AritmeticException extends Exception { }
class NumberFormatException extends Exception { }
function divide ($a, $b)
{
if( ! (is_int($a) and is_int($b)) )
{
throw new NumberFormatException();
}
if( $b === 0 )
{
throw new AritmeticException();
}
return ( $a / $b );
}
Example: catching your own exception
try
{
$divide = divide( 3, 3 );
}
catch ( NumberFormatException $e )
{
print "Please give integer-values";
}
catch ( ArithmeticException $e )
{
print "You cannot divide with 0";
}
Exception class
class Exception
{
protected $message = 'Unknown exception'; // exception message
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
function __construct($message = null, $code = 0);
final function getMessage(); // message of exception
final function getCode(); // code of exception
final function getFile(); // source filename
final function getLine(); // source line
final function getTrace(); // an array of the backtrace()
final function getTraceAsString(); // formated string of trace
function __toString(); // formated string for display
}
Making a better version of own Exceptions
class AritmeticException extends Exception
{
function __construct( $msg )
{
parent::__construct( $msg );
}
}
function divide ($a, $b)
{
if( ! (is_int($a) and is_int($b)) )
{
throw new NumberFormatException();
}
if( $b === 0 )
{
throw new AritmeticException("You cannot divide with zero");
}
return ( $a / $b );
}