try catch statement, need new set of eyes

Posted on 2009-02-23
Last Modified: 2012-05-06
Ok, I'm writing this project management type system out of boredom. It's framework is based off the Joomla 1.x framework even though I'm writing all of this from scratch. Anyway, I'll exclude all my code and just post a code example of what I'm trying to do here. Everything works except the try/catch part (the try block will execute fine, but if it encounters an error, it doesn't revert to the catch block).

What it's doing is reading a "cleansed" request variable called "component". It uses that to include the proper .php file from my /components/ directory, and create an instance of the class within this .php file and assign it to a property of my parent class... so that $this->component is now a pointer to the class inside of the included php file.

Now in my page template, in the content area, I simply call a $toDo->showContent() which is where I have a problem.. This function should first ATTEMPT to execute the components processRequest method (this is the included file remember).. however (this is purely for error reporting) if that method does NOT exist, OR the component wasn't included properly (for whatever reason), I'd like it to echo a generic error like "component does not follow coding guidelines"..

What happens is if the processRequest method exists, it is executed properly, however if I rename that method (to purposely make it try and execute a method that does not exist), it just shows a blank area where that content SHOULD have been (the rest of the page loads fine) and I get the PHP generated error "Call to undefined method com_pages::processRequests()".

Is this just improper use/understanding of how the try/catch system works or am I using it wrong or what? Hell even if I could do a

if typeof $this->component->processRequest is a method then execute else echo generic error. I just want a "fallback" if the method doesn't exist instead of a blank page that tells me nothing or a PHP generated error.
class toDo {

	public 		$component	= stdClass;


	public function processRequest($request) {

		$this->request = $this->cleanupRequest($request);



		$component = "com_" . $this->request["component"];

		$componentFile = $this->path . "/components/" . $component . ".php";


		switch($this->request["component"]) {


				if (file_exists($componentFile)) {


					$this->component = new $component();


				else {







	public function showContent() {

		try {



		catch (Exception $e) {

			echo "component does not follow coding guidelines";




	private function cleanupRequest($data) {

		$data["component"] = preg_replace("/[^a-zA-Z0-9_]/", "", $data["component"]);

		$data["component"] = trim($data["component"]);


		$data["component"] = ($data["component"] == "") ? "pages" : $data["component"];

		return $data;



Open in new window

Question by:MMDeveloper
    LVL 15

    Author Comment

    oh and sorry... $this->request is indeed declared as a public variable and contains the $_REQUEST data.
    LVL 19

    Accepted Solution

    The try/catch mechanism will not catch fatal errors such as the "call to an undefined method" however in this particular case you can use the function_exists function to check if the function is indeed present, if not you could raise a custom Exception which would be caught by the "catch" statement.

    Alternatively you could also try to use the set_error_handler function and define a custom error handler for errors.
    LVL 15

    Author Closing Comment

    I knew I needed a new set of eyes I've stared at this too long. I could use that or a usage or get_class_methods, either way, thank you for showing me the light.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    I worked at a US software company that used offshore contractors for ten years and offshore employees for three years. We had a positive experience and you can too.   When I interviewed people for positions in the US, I would tell them that we wor…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now