Exception Handling :

Exception Handling is the mechanism to handle exceptions by using of a try & catch block

What is an exception?

An exception is a logical/system error that occurs during the normal execution of a script. The exception could either be raised by the system or the program itself it the exception cannot be handled and the caller script/function needs to be informed about the same.

Syntax
    try{
	//codes they may raise exception
    }catch(Exception $e){
	echo "Message : " . $e->getMessage();
	echo "Code : " . $e->getCode();
    }
    
example:
    <?php
    try {
		check();
    }
    catch(Exception $e) {
		echo "Message : " . $e->getMessage();
		echo "Code : " . $e->getCode();
    }
    function check() {
		if(some error condition) {
		    throw new Exception("Error String",Error Code);
		}
    }
    ?>
Description :

In the above example, the method check() is called between the try {} block. The try{} block is the area where you will place your code that could raise an exception. Below the try{} block is the catch() {} block. The catch block expects the Exception type of object as a parameter. Within the catch() {} block you will place your logic to either fix the issue or log the error. In the function check(), we raise an Exception using the 'throw' keyword. The statement following 'throw' is the syntax of creating a new object of Exception type. The exception class accepts two parameters. The left parameter is a string that is the error message and the right parameter is the integer error code that you wish to assign to that error.

User defined and nested exception :

We can throw user defined exception by using keyword throw ,
but the thrown object must be an instance of the Exception class or a subclass of Exception. Trying to throw an object that is not will result in a PHP Fatal Error.

When there are chances of getting Exceptions inside one try (Exception Handling) block , we can use nested exception handling blocks .

Syntax
    try{
	try{
	    //codes they may raise exception or thrown
	}catch(Exception $e){
	    //Code to handle the exception raised or to throw
	}
    }catch(Exception $e){
	echo "Message : " . $e->getMessage();
	echo "Code : " . $e->getCode();
    }
    
example:
<?php
class MyException extends Exception { }

class Test {
    public function testing() {
        try {
            try {
                throw new MyException('foo!');
            } catch (MyException $e) {
                /* rethrow it */
                throw $e;
            }
        } catch (Exception $e) {
            var_dump($e->getMessage());
        }
    }
}
$foo = new Test;
$foo->testing();
?>

OUTPUT : string(4) "foo!"
Description :

In the above example, we have created our own user defined Exception class named "MyException" by extending Exception super class. The try{} block is the area where you will place your code that could raise an exception. Below the try{} block is the catch() {} block. But here in this case we do have nested try block (for throwing user defined exception) The catch block expects the Exception type of object as a parameter. Within the catch() {} block you will place your logic to either fix the issue or log the error.

After editing Click here:
Output:
Hello World