Hi guys. In my script I have a number of dependent executions. Some are db related, some are not.
I am trying to implement the most efficient error handling methodology I can, according to my current capability, but something is bugging me in terms of the script sequence.
I wrap each code block in a try/catch, but then for subsequent, I have to either nest the try/catches or wrap each in an if(); statement.
What would make more sense to me would be the ability to have multiple try{} blocks that throws to an end catch(), jumping over all other code. I am currently lookin into the goto method, but not sure if this is best practice?
Here is an example of what I mean:
try {
// execute code block.
// If all good, script continues to next try{}
// If it breaks, throws to catch.
} catch(Exception $e) {
$errorMsg[] = $e->getMessage();
// ** My problem now is that I don't want the next 2
// ** code blocks to be executed so I have to wrap
// ** them in an if() or nest these try{}'s.
// ** This is okay for a couple of code blocks
// ** but looks messy when there are many
}
if(count($errorMsg) === 0) {
try {
// execute 2nd code block
} catch(Exception $e) {
$errorMsg[] = $e->getMessage();
}
}
if(count($errorMsg) === 0) {
try {
// execute 3rd code block
} catch(Exception $e) {
$errorMsg[] = $e->getMessage();
}
}
My thought on the goto would be to include it in each catch() and sent it to the appropriate label: jumping whatever code it should.
I hope my question makes sense.
Regards
/Danny