M.O.S. Studios Posted Friday at 02:50 PM Share Posted Friday at 02:50 PM 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted Friday at 03:47 PM Share Posted Friday at 03:47 PM 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. Quote Link to comment Share on other sites More sharing options...
M.O.S. Studios Posted Friday at 04:18 PM Author Share Posted Friday at 04:18 PM (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 Friday at 04:18 PM by M.O.S. Studios Quote Link to comment Share on other sites More sharing options...
requinix Posted Friday at 05:09 PM Share Posted Friday at 05:09 PM 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. Quote Link to comment Share on other sites More sharing options...
gizmola Posted Friday at 05:13 PM Share Posted Friday at 05:13 PM Try catch blocks should only be used for situations where exceptions are expected, and from which you can recover and proceed. Quote Link to comment Share on other sites More sharing options...
maxxd Posted Friday at 08:48 PM Share Posted Friday at 08:48 PM 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.