Php: catch exception and continue execution, is this possible?

Is it possible to throw an exception and continue executing the script?

+45
php
Jan 25
source share
6 answers

Of course, just catch the exception where you want to continue execution ...

try { SomeOperation(); } catch (SomeException $e) { // do nothing... php will ignore and continue } 

Of course, the problem is to silently discard what could be a very important mistake. SomeOperation () may fail, causing other subtle, difficult definition problems, but you will never know if you throw an exception.

+61
Jan 25
source share

Yes, but it depends on what you want to accomplish:

eg.

 try { a(); b(); } catch(Exception $e){ } c(); 

c() will always be executed. But if a() throws an exception, b() fails .

Put the material in a try block, which is dependent on each other. For example. b depends on some result of a , it makes no sense to put b after the try-catch .

+95
Jan 25 '10 at 14:12
source share

Sure:

 try { throw new Exception('Something bad'); } catch (Exception $e) { // Do nothing } 

You may need to read the PHP documentation on Exceptions .

+12
Jan 25
source share

Yes.

 try { Somecode(); catch (Exception $e) { // handle or ignore exception here. } 

however, note that php also has error codes, separate from exceptions, inherited hold before php had primitive primitives. Most built-in libraries still throw error codes, not exceptions. To ignore the error code, call the function with the @ prefix:

 @myfunction(); 
+5
Jan 25 '10 at 14:10
source share

Another angle on this returns an Exception NOT throwing it from the processing code.

I needed to do this with the template I am writing. If the user tries to access a property that does not exist in the data, I return an error from the depth of the processing function, and do not throw it.

Then, in the calling code, I can decide whether to return the returned error by forcing try () catch () or just continue:

 // process the template try { // this function will pass back a value, or a TemplateExecption if invalid $result = $this->process($value); // if the result is an error, choose what to do with it if($result instanceof TemplateExecption) { if(DEBUGGING == TRUE) { throw($result); // throw the original error } else { $result = NULL; // ignore the error } } } // catch TemplateExceptions catch(TemplateException $e) { // handle template exceptions } // catch normal PHP Exceptions catch(Exception $e) { // handle normal exceptions } // if we get here, $result was valid, or ignored return $result; 

As a result, I still get the context of the original error, even if it was selected at the top.

Another option might be to return a custom NullObject or UnknownProperty object and compare it with before deciding to disable catch (), but since you can re-throw errors anyway, and if you have full control over the overall structure, I think this is a neat way around the problem of inability to continue trying / catching.

0
Aug 12 '13 at 19:35
source share

An old question, but the one that I had in the past when moving away from VBA scripts to php, where you could have us “GoTo” re-enter the cycle “On Error” with “Resume” and away it would still be processing the function.
In php, after some trial and error, I now use the nested try {} catch {} for critical or non-critical processes or even for interdependent class calls so that I can trace the path to the start of the error. for example, if function b depends on function a, but function c is good, but should not stop the process, and I still want to know the results of all 3 no matter what I do:

 //set up array to capture output of all 3 functions $resultArr = array(array(), array(), array()); // Loop through the primary array and run the functions foreach($x as $key => $val) { try { $resultArr[$key][0][] = a($key); $resultArr[$key][1][] = b($val); try { // If successful, output of c() is captured $resultArr[$key][2][] = c($key, $val); } catch(Exception $ex) { // If an error, capture why c() failed $resultArr[$key][2][] = $ex->getMessage(); } } catch(Exception $ex) { // If critical functions a() or b() fail, we catch the reason why $criticalError = $ex->getMessage(); } } 

Now I can skip my array of results for each key and evaluate the results. If there is a critical failure for () or b ().
I still have a benchmark about how far it reached before a critical crash occurred in $ resultArr, and if the exception handler is set correctly, I know that it was () or b () that failed.
If c () fails, the loop continues. If c () did not succeed at different points, with a small amount of additional message loop logic, I can even find out if c () worked or had an error at each iteration by polling $ resultArr [$ key] [2].

0
Jul 28 '17 at 13:01
source share



All Articles