Jump to content

Try Scope?


Recommended Posts

Hey Everyone,

Odd question. I am working with a class that runs methods when a new instance is created. Is there a way to catch any errors thrown within the methods?

Here is the code:

function __construct(){
	try{
            $this->val1 = $this->func1;
            $this->val1 = $this->func2;
            $this->val1 = $this->func3;
        }catch(Exception $e){
            $this->notes($e);
            $this->errors = true;
        }
}

Ideally, if method "func1" throws an error at anypoint within it, then it will be caught by "catch" in _construct.

 

Is this possible, or do I need to add "try/catch" to each method?

Link to comment
Share on other sites

55 minutes ago, M.O.S. Studios said:

Ideally, if method "func1" throws an error at anypoint within it, then it will be caught by "catch" in _construct.

What do you mean "ideally"? That is how it will work: if any of those three methods throws an exception then the try/catch will catch it. Because that's what it does.

Link to comment
Share on other sites

Posted (edited)

I might have worded it wrong.

 

I want it so that if any of those three methods have an exception, it will bubble up to the try/except in the _construct method.

 

for example:

class myclass{

	public function func1(){
		//line of code that creates fatal error
	}


	function __contruct(){
		try(){
			$this->func1();
		}catch(Exception $e){
			echo "found a error";
		}
	}


}

the above will work only if the exception is thorwn within the scope of func1(). if func1() calls an external function that throws an exception, it wont catch it. It doesn't "bubble up".

 

if I change it to:

class myclass{

	public function func1(){
		try(){
			buggyFunc();
		}catch(Exception $e){
			 throw new Exception($e);
		}
	}


	function __contruct(){
		try(){
			$this->func1();
		}catch(Exception $e){
			echo "found an error";
		}
	}
}

this works.

The problem is, if you look back to my original example: I don't want any subsequent functions to run if the previous function had an error. At the same time, I don't want to a script filled with "try/catch/throw" commands

Edited by M.O.S. Studios
Link to comment
Share on other sites

49 minutes ago, M.O.S. Studios said:

if func1() calls an external function that throws an exception, it wont catch it. It doesn't "bubble up".

Yes it will. If something throws an exception, it bubbles up to the closest try/catch, and if that doesn't handle it (or throws it again) then it continues up to the next-closest try/catch, and so on. Doesn't matter what functions those try/catch blocks are in.

Link to comment
Share on other sites

This is how exceptions work. You can always try it by intentionally throwing an exception from a method called within a try/catch in a different method. It'll catch - if it doesn't seem to, make sure you've got error reporting enabled.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.