PHP Exception Handling

Exception Handling in PHP programming is almost similar to exception handling in all programming languages. Before PHP 5, exception handling did not exist. In PHP 7 exception handling is the "rule". As a developer, you need to do all to ensure that your program will not crash.

Exception provides a unified mechanism for handling errors in an extensible, maintainable and object-oriented way. In exception handling, code is executed inside try catch code. The code is enclosed within a try block, to facilitate the catching of prospective exceptions. If something goes wrong inside the try block, you can then throw the exception, and your code will then catch the exception and respond accordingly.

PHP error handling keywords

PHP provides the following keywords for the exception handling purpose -

  • try: The try block contains the code in which an exception can arise.
  • catch: This block of code will be called only if an exception occurs within the try code block.
  • throw: It is used to throw an exception. Each "throw" must have at least one "catch".
  • finally: Code within the finally block will always be executed after the try and catch blocks, regardless of whether an exception has been thrown, and before normal execution resumes.

Syntax

try {
	//Throw an exception
	throw new exception('message', code);
} catch(exception) {
	// handle exception
}

Example 1

<?php
try { 
	throw new Exception('An error has occured', 42);
}
catch(Exception $e)
{
	echo 'Exception '.$e->getCode().', '.$e->getMessage().
		 ' in filename '.$e->getFile().' on line number '.$e->getLine();
}
?>		 

The throw keyword triggers the exception handling mechanism. Each try block of your code needs at least one catch block. A single try block can have more than one catch block to catch different types of exceptions.



Example 2

<?php
try { 
	throw new Exception('An error has occured', 42);
}
catch(Exception1 $e)
{
	echo 'Exception '.$e->getCode().', '.$e->getMessage().
		 ' in filename '.$e->getFile().' on line number '.$e->getLine();
}
catch(Exception2 $e)
{
	echo 'Exception '.$e->getCode().', '.$e->getMessage().
		 ' in filename '.$e->getFile().' on line number '.$e->getLine();
}
?>		 




Exception Class

The Exception class has the following built-in methods.

Name Returns
getCode() It returns the code of exception.
getMessage() It returns the message of exception.
getFile() It returns full path of the source file where the exception was raised.
getLine() It returns the line number in the code where the exception was raised.
getTrace() It returns an array of the backtrace where the exception was raised.
getTraceAsString() It returns information in a formatted string of trace.

User defined Exception

We can define our own exception class. For this, we need to extend the Exception class.

Example

<?php
class customException extends Exception {
	public function msg(){
		$msg = 'Wrong Answer';
		return $msg;
	}
}
$ans = 4;
try { 
	if($userinput != $ans) {
		throw new customException('An error has occured', 42);
	}
}
catch(customException $e)
{
	echo 'Exception '.$e->getMessage();
}
?>		 




PHP exception finally block

The code within finally block will always be executed after the try catch block.

Example

<?php
try { 
	throw new Exception('An error has occured', 42);
}
catch(Exception $e)
{
	echo 'Exception '.$e->getCode().', '.$e->getMessage().
		 ' in filename '.$e->getFile().' on line number '.$e->getLine();
}
finally {
	echo 'This block is always executable.';
}
?>		 

PHP set_exception_handler() function

The set_exception_handler() function is used to set the user defined exception handler function. The script stops execution after this function is called.

function setException() {
	try { 
	throw new Exception('An error has occured', 42);
	}
	catch(Exception $e)
	{
		echo 'Exception '.$e->getCode().', '.$e->getMessage().
			 ' in filename '.$e->getFile().' on line number '.$e->getLine();
	}
}
set_exception_handler(setException);

PHP restore_exception_handler() function

In PHP, the restore_exception_handler() function is used to restore the previous exception handler after changing it through set_exception_handler() function.

function setException() {
	try { 
	throw new Exception('An error has occured', 42);
	}
	catch(Exception $e)
	{
		echo 'Exception '.$e->getCode().', '.$e->getMessage().
			 ' in filename '.$e->getFile().' on line number '.$e->getLine();
	}
}
set_exception_handler(setException);
restore_exception_handler()




Read more articles


General Knowledge



Learn Popular Language