Catching Exceptions in PHP 5 : Exception « Statement « PHP






Catching Exceptions in PHP 5

 
<?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";
        }
?>
  
  








Related examples in the same category

1.extends Exception to create your own exception
2.Using PHP 5 exception handling
3.Using Custom Exceptions to Handle Different Circumstances