[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

Try{} Catch {} else?

I have a try catch in my code:
try{
    //Try Something
}
catch (Exception $e) {
    echo "<h3>ERROR:</h3> {$e->getMessage()} (Error Code: {$e->getCode()})";
}

Is there a way to throw an else in there so that I can execute some code, if no errors were caught. Something like:
catch (Exception $e) {
    echo "<h3>ERROR:</h3> {$e->getMessage()} (Error Code: {$e->getCode()})";
} else {
    //There were no Errors, execute some success code.
}
0
interclubs
Asked:
interclubs
  • 2
  • 2
  • 2
  • +1
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
no, you simply continue that code in the try:


try{
    //Try Something

   try {
   //There were no Errors, execute some success code.
   }
   catch (Exception $e) {
   }
}
catch (Exception $e) {
    echo "

ERROR:

{$e->getMessage()} (Error Code: {$e->getCode()})";
}
0
 
asafadisCommented:
Actually... if no errors are caught, it will just run whatever is inside the "try" section.
Hypothetically, if there were a "try" section, what scenario would make it skip both the "try" and "catch" section?
0
 
Aleksandar BradarićSoftware DeveloperCommented:
Maybe just put the "else" code after the " //Try Something" code? It will proceed to it if nothing is "caught" :)
try{
    //Try Something
    echo "<h3>SUCCESS:</h3>";
}
catch (Exception $e) {
    echo "<h3>ERROR:</h3> {$e->getMessage()} (Error Code: {$e->getCode()})";
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
me655321Commented:
You could do something like this
try{
    $errorcaught=false;
}
catch (Exception $e) {
    echo "<h3>ERROR:</h3> {$e->getMessage()} (Error Code: {$e->getCode()})";
    $errorcaught=true;
}
if (!$errorcaught){
    //Success code
}

Open in new window

0
 
asafadisCommented:
I still don't get what's the point.  A try/catch will either run what's inside the "try" or what's inside the "catch" every single time.
There is no third possibility.
0
 
me655321Commented:
Right, I don't get the point either....but then again I never got the point of having a "finally" but a lot of languages include that.
0
 
Aleksandar BradarićSoftware DeveloperCommented:
The point is that not the whole TRY section gets executed if something is caught and sometimes you just want to know that or do an action after it, but you're right - it can be accomplished in other ways :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now