PHP Exception Handling


PHP Exception Handling :– With PHP 5 came a new object oriented way of dealing with errors. Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception.

Exception is generated by abnormal behaviour of program; once it happens in your program it will stop the normal flow of your program.Hence in order to maintain programms flow a programmer needs to capture these errors visi at any point of time for continuously flow of rest part of code.

An exception is a signal that indicates some sort of exceptional event or error has occurred.


Try VS CATCH BLOCK

  1. Try block is used to check a perticular block of code where a programmer expects a possibility of exception whereas Catch block is used to handle an exception
  2. Here we keep one important thing to keep in mind while program a computer is where to write try block to check regular exceptions.

PHP Exception | Example

Throwing an Exception

Example

<?php
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo "Hello World\n";
?>

Exception handling with a finally block

Example

<?php
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
    echo "First finally.\n";
}

try {
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
    echo "Second finally.\n";
}

// Continue execution
echo "Hello World\n";
?>

Creating a Custom Exception Class

To create a custom exception handler you must create a special class with functions that can be called when an exception occurs in PHP. The class must be an extension of the exception class.

The custom exception class inherits the properties from PHP’s exception class and you can add custom functions to it.

Lets create an exception class:

Example

<?php

class Emailexception extends Exception{
	
}
	$email="smith.johnhotmail.com";

try{
	
	if($email==""){
		Throw new Emailexception("Email Address is invalid");
	}
	if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {           
        throw new Emailexception("<p><b>$email</b> wrong E-mail address!</p>");
	}
	echo "correct email address";
}
catch(Emailexception $ex){
	
	echo $ex->getMessage();
}
?>

Re-throwing Exceptions

try/catch blocks can be nested. Sometimes you’ll want to catch an exception, look at some of its properties, and then throw it again to let a parent catch block handle it. This can often be useful to check an error condition and decide whether it should be fatal or not. This example code demonstrates re-throwing an exception:

Example

<?php
class MultiCatch extends Exception{
	function handleException(){
		echo "<br>";
		echo "this is the nested exception example ";
		echo "<br>";
	}
}
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
    echo "First finally.\n";
}

try {
    echo inverse(0) . "\n";
} catch (Exception $e) {
	
	$multicatch=new MultiCatch();
	throw new MultiCatch($multicatch->handleException());
    //echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
	echo "<br>";
    echo "Second finally.\n";
}

// Continue execution
echo "Hello World\n";
?>

Advertisements

Add Comment

📖 Read More