0

I want to stop code execution (all application code). Actually I manage this code:

try
    {
        $connection = mysqli_connect(   $this->_host,
            $this->_username,
            $this->_dbPass,
            $this->_dbName  );

        if(mysqli_connect_errno())
        {
            throw new Exception("problem in connection.");
        }
    }
    catch(Exception $ex)
    {
        header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
        echo json_encode(array("success" => false, "message" => $ex->getMessage()));
        return;   //<- application don't end
        //exit(); //<- application end
     }

Now if I call another method after the above code block that prints "hello world" with the return statement I get the echo, but if I use exit() I don't see any echo. So my question is: throw a new exception don't stop the application instance? I must use exit() for stop it after the exception instead of return?

Sevengames Xoom
  • 312
  • 1
  • 6
  • 18
  • Or die(); You are just setting the header but you must specify that your code should stop. – ka_lin Feb 18 '16 at 19:52
  • you suggest to use die or exit? – Sevengames Xoom Feb 18 '16 at 19:52
  • 1
    *Thinking outloud here*: `exit( json_encode(array("success" => false, "message" => $ex->getMessage())); );` – Funk Forty Niner Feb 18 '16 at 19:53
  • I think die, sound cool too :) – ka_lin Feb 18 '16 at 19:54
  • Do you want a more concise answer than "yes" ? – scrowler Feb 18 '16 at 19:54
  • http://stackoverflow.com/questions/1795025/what-are-the-differences-in-die-and-exit-in-php – ka_lin Feb 18 '16 at 19:55
  • @Fred-ii- thinking outloud? what? – Sevengames Xoom Feb 18 '16 at 19:55
  • `exit();` does accept an argument. http://php.net/manual/en/function.exit.php so not 100% sure if my suggestion would work. – Funk Forty Niner Feb 18 '16 at 19:56
  • @SevengamesXoom: Another way of proposing a solution (humor) – ka_lin Feb 18 '16 at 19:56
  • Is this code inside a function? Why would you expect returning from a function to exit the entire application? – David Feb 18 '16 at 19:56
  • 1
    @KA_lin haha I'm not english so it's difficult for me understand some humor :P – Sevengames Xoom Feb 18 '16 at 19:56
  • @David yes is inside a function... anyway I just replaced with die(); – Sevengames Xoom Feb 18 '16 at 19:57
  • @SevengamesXoom: Well, returning from a function just stops *that function*. I don't know of any programming language where that would stop *the entire application* from continuing. – David Feb 18 '16 at 19:58
  • 1
    @SevengamesXoom: Additionally, you're making entirely incorrect use of exceptions here. If you want to perform the logic that's in the `catch` block in the event of a SQL connection error, just put that logic inside of your `if` block. There's no need for an exception here at all. As a general rule, never use exceptions for logic flow. – David Feb 18 '16 at 19:59
  • @David This code is part of RestFul API... so I must stop the execution before of do anything – Sevengames Xoom Feb 18 '16 at 20:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103894/discussion-between-sevengames-xoom-and-david). – Sevengames Xoom Feb 18 '16 at 20:02
  • @SevengamesXoom: Those two statements have nothing to do with one another. But if you want to end the script then, well, you *already know how* in the question itself. `exit();` So... what exactly are you asking? "If I use `exit()` then the script ends. But if I don't, it doesn't end. How do I make the script end?" That makes no sense. – David Feb 18 '16 at 20:03
  • @David well I thought that with throw automatically the script's die, but I was wrong... – Sevengames Xoom Feb 18 '16 at 20:06
  • @SevengamesXoom: If an exception isn't *caught* then it would end the script. But you explicitly catch the exception in your code. (And I really can't stress enough how much you *shouldn't* even be using exceptions here.) – David Feb 18 '16 at 20:06
  • @David yes, but for end the script I must add `die()` – Sevengames Xoom Feb 18 '16 at 20:07
  • @SevengamesXoom: Or `exit()`, which you already knew because it's in the code you posted. – David Feb 18 '16 at 20:08
  • @David yes, I see the difference between `die` and `exit` and are equal I guess, why php use two equal function? – Sevengames Xoom Feb 18 '16 at 20:09
  • 1
    PHP has a lot of aliases for common functions, mostly to assist people coming from other languages. – Devon Bessemer Feb 18 '16 at 20:10

1 Answers1

4

There has been quite a discussion in the comments, but to answer the question at hand, a return does stop execution of the script in the global scope.

http://sandbox.onlinephpfunctions.com/code/59a49286b1cbb62e92fd95134593c2da5ef94468

Example:

<?php

try {
    throw new Exception('Hello');
}
catch (Exception $e) {
    return;
}
echo "hello world"; // Not reached

?>

An exception will stop execution if it is uncaught. Otherwise, if the exception is caught, the same rules apply. In the global scope, return will exit the application, inside a function or method, a return will only exit the function.

exit() or die() will both exit the application no matter what scope they are called in.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95