1

I am new to php, i am from java background, i am wondering why php doesn't goes directly when exception occured in try block without throwing that exception manually. e.g.

 <?php
//create function with an exception
function checkNum($number) {
  if($number/0) {
    throw new Exception("Value must be 1 or below");
  }
  return true;
}

//trigger exception in a "try" block
try {
  checkNum(2);
  //If the exception is thrown, this text will not be shown
  echo 'If you see this, the number is 1 or below';
}

//catch exception
catch(Exception $e) {
  echo 'Message: ' .$e->getMessage();
}
?>

in above example in if condition the divide by zero exception is occured and then it will directly go to the catch block instead it goes to inside if.why?

4
  • 2
    PHP's built-in error checking doesn't throw exceptions. Commented May 25, 2017 at 6:31
  • But my question is ,yes i want to catch it but i don't want to execute if block if exception is in line where i am checking for division , if i remove the throw inside if then it will not goes to catch block instead it will execute next code , still having exception in if statement. Commented May 25, 2017 at 6:36
  • 1
    It's not executing the if() block. $number/0 returns false because of the division by 0, so it doesn't execute the if(). Then the function returns true, and it prints the If you see this message. Commented May 25, 2017 at 6:43
  • Why are you dividing the number by 0 to tell if it's 1 or below? Commented May 25, 2017 at 6:43

1 Answer 1

4

The code you posted doesn't do what you say it does.

When you execute:

if ($number/0)

the division by zero prints a warning, and then returns false. Since the value is not truthy, it doesn't go into the if block, so it doesn't execute the throw statement. The function then returns true. Since the exception wasn't thrown, the statement after the call to checkNum(2) is executed, so it prints the message.

When I run your code, I get the output:

Warning: Division by zero in scriptname.php on line 5
If you see this, the number is 1 or below

PHP doesn't use exceptions for its built-in error checks. It simply displays or logs the error, and if it's a fatal error it stops the script.

This has been changed in PHP 7, though. It now reports errors by throwing an exception of type Error. This isn't a subclass of Exception, so it won't be caught if you use catch (Exception $e), you'll need to use catch (Error $e). See Errors in PHP 7. So in PHP 7 you could write:

<?php
//create function with an exception
function checkNum($number) {
  if($number/0) {
    throw new Exception("Value must be 1 or below");
  }
  return true;
}

//trigger exception in a "try" block
try {
  checkNum(2);
  //If the exception is thrown, this text will not be shown
  echo 'If you see this, the number is 1 or below';
}

//catch exception
catch(Error $e) {
  echo 'Message: ' .$e->getMessage();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank's @Barmar, you save my day. since two days of bug... no solution....Thank's

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.