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?

23
  • Or die(); You are just setting the header but you must specify that your code should stop. Commented Feb 18, 2016 at 19:52
  • 1
    Thinking outloud here: exit( json_encode(array("success" => false, "message" => $ex->getMessage())); ); Commented Feb 18, 2016 at 19:53
  • 1
    @KA_lin haha I'm not english so it's difficult for me understand some humor :P Commented Feb 18, 2016 at 19:56
  • 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. Commented Feb 18, 2016 at 19:59
  • 1
    PHP has a lot of aliases for common functions, mostly to assist people coming from other languages. Commented Feb 18, 2016 at 20:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

i removed my own answer because this one is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.