mysql - PHP: How to execute try block even if exception found? -
i have try block in exceptions definitely thrown don't want stop execution of try block if happens.
this code:
try { $querystr = "select * `$current_table` ..."; $query = $db->prepare($querystr); $query->execute(); while($row = $query->fetch()) { } } catch(pdoexception $e) { //echo $e->getmessage(); }
this try/catch block inside loop, $current_table
changing continously. current table can take 4 values april-june-2014
, july-september-2014
, october-december-2014
, jan-march-2015
. these table names stored in array.
now when query first table , if found, it's , if not found want check other tables , see if exists. if of table not found, throw exception , won't check other tables.
is there way that?
use continue statement:
for (/* condition */) { try { // things } catch (exception $e) { continue; } // won't reach here if exception caught }
example (try out on writecodeonline):
$counter = 0; ($i = 0; $i < 5; $i++) { try { $counter++; if ($counter != 3) { throw new exception("test"); } } catch (exception $e) { continue; } echo "this prints when exception not thrown (when \$counter 3). see: \$counter = " . $counter; } echo "\n"; var_dump($counter);
Comments
Post a Comment