PHP 7 Exception Handling

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.

Syntax

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

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();
}
?>		 

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

Example

<?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

Exception class has following built in methods.

Name Returns
getCode() returns code of exception
getMessage() returns message of exception
getFile() returns full path of the source file where the exception was raised
getLine() returns the line number in the code where the exception was raised
getTrace() returns an array of the backtrace where the exception was raised
getTraceAsString() returns information in 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();
}
?>		 




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.';
}
?>		 

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);

restore_exception_handler() function

It 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