CopyPastehas never been so tasty!

Exception Handling in PHP

by anonymous

  • 0
  • 0
  • 0
79 views

Orignal Source:http://codingvilla.com/exception-handling-php-single-article-582.aspx#

Exception handling:

 

Exceptions are the abnormal behavior of your code that was occurring unexpectedly. Exception handling is used to handle this type of error in your code.

 

Exception Handling in PHP:

 

To handle unexpected error or exceptions in php we use exception handling class. We can through and catch exception in php. For this we use try catchblock. This code uses exception class and through and catch exceptions.

 

Code:

 

<?php

        class MyException extends Exception {

        }

        class AnotherException extends Exception {

        }

        class ThrowExample {

                public function makeMyException() {

                        throw new MyException();

                }

                public function makeAnotherException() {

                        throw new AnotherException();

                }

                public function makeError() {

                        throw new Exception();

                }

        }

        $inst = new ThrowExample();

 

        try {

                $inst->makeMyException();

        } catch(MyException $e) {

                echo "Caught 'MyException'\n";

        }

        try {

                $inst->makeAnotherException();

 

        } catch(MyException $e) {

                echo "Caught 'MyException'\n";

        } catch(AnotherException $e) {

                echo "Caught 'AnotherException'\n";

        }

 

        try {

                $inst->makeError();

        } catch(MyException $e) {

                echo "Caught 'MyException'\n";

        } catch(AnotherException $e) {

                echo "Caught 'AnotherException'\n";

        }

?>

 

Above is a simple code that shows the exception handling in php.

 

This simple article tells that how we can use exception handling in php.

Add A Comment: