4

I have this code:

function divide($a,$b){
    try{
        if($b==0){
            throw new Exception("second variable can not be 0");
        }
        return $a/$b;
    }
    catch(Exception $e){
        echo $e->getMessage();
    }
}

echo divide(20,0);
echo "done";

It throws an exception when the second parameter is 0. How can I stop done from printing?

1
  • 1
    don't catch the exception, or have the catch block die/exit. Since you're catching it, PHP can assume the error's been handled and will proceed on wards. Since you're not DOING anything with the error, you've basically got the PHP equivalent of visual basic's "on error resume next". Commented Jun 18, 2014 at 15:14

1 Answer 1

6

Don't catch your exception in divide() and catch it later:

function divide($a,$b){
    if($b==0){
        throw new Exception("second variable can not be 0");
    }
    return $a/$b;
}

try {
    echo divide(20,0);
    echo "done";
} catch(Exception $e){
    echo $e->getMessage();
}
Sign up to request clarification or add additional context in comments.

Comments

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.