php - why does a caught exception still break a loop? how can we make it continue? -


I have given an example of this issue that I am running, in my (possibly incorrectly) way of thinking, a catching exception Should not cause the loop to break; This means that the loop faces an exception, throws it, the exception is handled, and then the loop should continue, will not it? The problem is that this is not happening, loop is breaking on its first exception, even though it is in an attempt / hold block I am misunderstanding here? How can I make this work as expected (I hope both exceptions will be handled and A, B, C, D properties will be set).

I have created a demo here:

Code:

  & lt; Php abstract class xGrandparent {public function __construct ($ params = array) {foreach ($ c => $ params as $ v) {if (Property_exists ($ this, $ k)) {$ this- & Gt; $ K = $ v; } And {switch ($ k) {case 'e': throw a new exception ('exception e', 111); break; Case 'F': Throw New Exception ('Exception F', 222); break; }}}}} Abstract class xgrandparent {protected $ c, $ d; Public Function __ Conversions ($ params = array) {Try (Original: __ Construction ($ params);} Hold (exception $ E) {if ($ e-> getCode () == 222) { Resonant "*** Parents caught exceptions \ n";} and {throw $ E;}}}} square x child xParent extended {protected $ a, $ b; public function __ conversion ($ params = array ()) { Try (Original: __ Construction ($ params);} Hold (exception $ E) {Echo "*** hair caught exception} \ n";}}} $ Test = new xChild (array ('a' = & gt; ; 'One wall', 'c' = & gt; 'c val', 'e' => e val ',' f '=>' f-val ',' b '=' gt; Wall ' , 'D' = & gt; 'D Well',)); Var_dump ($ test);   

Output:

  *** Exception object (xChild) # 1 (4) {["a: protected"] => string (5) "one val" ["b: protected"] => NULL ["c: protected"] = (5) "C Well" ["D: Protected"] = & gt; NULL}    

OK - The point is that apparently the exception is "just do not work this way" I have solved this kind of problem, hopefully it helps someone:

class xGrandParent {protected $ propertySettingExceptions = array (); $ {If property_exists ($ this, $ k)) {$ this- & gt;} as public function __ conversion ($ params = array ()) {foreach ($ c => $ v) $ K = $ v; } Else {try {switch ($ k) {case 'e': throw a new exception ('exception e', 111); break; Case 'F': Throw New Exception ('Exception F', 222); break; }} Hold (exception $ E) {$ this-> Property Setting Exceptions [] = $ e; }}}}} Abstract class xgrandparent {protected $ c, $ d; Public function __ composition ($ params = array ()) {origin: __ generation ($ params); {(Size exceptions ($ exception = & amp; $ this-> property settings exception)} {foreach ($ k => exception as exception) {switch ($ exception-> getCode ()) {Case 222: Echo "* ** Guardian caught the exception \ n"; Not set ($ exception [$ K]); break; }}}}} Class x preserves the child's xprint {protected $ a, $ b; Public function __ composition ($ params = array ()) {origin: __ generation ($ params); If (size exceptions ($ Exception = & amp; $ this- & gt; Properties Setting Exception)) {foreach ($ k => $ Exception as Exceptions) {Switch ($ exception-> GetCode () } {Case: 111 "echo" ** hair caught exception \ n "; not set ($ exception [$ K]); breakage;}}}}} $ Test = new x channel (array ('A' = & gt; ; 'One wall', 'c' = & gt; c val ',' e '= & gt;' e-val ',' f '= & gt; f-val,' b '= & gt; B WAL ',' D '= & gt;' DYEL ',)); Var_dump ($ test);

Output:

  ** * The guard caught the exceptions *** Child caught exception object (xChild) # 1 (5) {["a: protected"] => ST The string (5) "C val" ["D:": "A val" ["B: protected"] => string (5) "b val" ["c: protected"] = & gt; Protected "] => string (5)" d val "[" propertySettingExceptions: protected "] => array (0) {}}    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -