PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
class cTest
{
public function __construct()
{
}
public function foo1()
{
self::foo2();
}
public function foo2()
{
throw new Exception("error...", 456);
}
}
try
{
$o = new cTest();
$o->foo1();
}
catch (Exception $e)
{
echo $e->getMessage();
}
foo2() throws an exception which gets passed to foo1().
In this code, foo1() throws exception to the try block - so its fine.
But, is it necessary to have have a try-catch-throw in foo1() too ?
No you don't need try/catch in foo1() but people commonly catch the execption anyway and then throw it again. You'll see this sort of thing in Java a lot.
/**
* Does the one thing.
* @throws Exception
*/
function foo1() {
self::foo2();
}
/**
* Does the two thing.
* @throws Exception
*/
function foo2() {
throw new Exception("I'm an exception");
}
If you know how to handle the exception you could write the try/catch in your Foo1 method and handle it there...
If you don't know how to handle the exception, simply let it bubble up.... (And handle it eg: in your code as you did right now)