class MyException extends Exception {
}
class AnotherException extends MyException {
}
class Foo {
public function something() {
throw new AnotherException ( );
}
public function somethingElse() {
throw new MyException ( );
}
}
$a = new Foo ( );
try {
try {
$a->something ();
} catch ( AnotherException $e ) {
$a->somethingElse ();
} catch ( MyException $e ) {
print "Caught Exception";
}
} catch ( Exception $e ) {
print "Didn't catch the Exception!";
}
key point to note in here is exception thrown in catch block wont be caught if its not surrounded by try block.
comments to answer.
The above code prints :
ReplyDeleteDidn't catch the Exception!
because, MyException catch block is not surrounded by try block.