PHP Exception Handling

Associate
Joined
23 Oct 2002
Posts
1,525
Location
That London, née Brighton
I've been coding in PHP for a very long time but only recently got into doing things the OO way due to being a retard. The OO way does appear to be a very lovely way.

Got a (probably simple) question about Exception Handling though. Let's say I have three classes, like so...

PHP:
class ClassThree {
	private $obj;

	function __construct() {
		try {
			$this->obj = new ClassTwo();
		}
		catch(Exception $e) {
			print 'Sorry guy, the bad stuff happen ('.$e->message.')';
		}
	}
}

class ClassTwo {
	private $obj;

	function __construct() {
		try {
			$this->obj = new ClassOne();
		}
		catch(Exception $e) {
			throw $e;
		}
	}
}

class ClassOne {

	function __construct() {
		throw new Exception('oh no and error message');
	}
}

... do I need that "intermediary" try/catch clause in ClassTwo, to re-throw the Exception from ClassOne, or if I leave it out will the Exception naturally keep getting passed back up the chain until it reaches ClassThree's try/catch?
 
Ok I've now actually tried this and no, I don't need the middle re-thrower, but, is it good/bad practice to have re-throw-er clauses in intermediary classes?
 
Ok so the general rule to apply is, you only catch it if you need to do something with it. Makes sense.
 
Back
Top Bottom