Uncaught Exception 'Exception' PHP

Asked

Viewed 1,299 times

1

Colleagues. I’ve adjusted the code. The solution was based on colleagues putting the new Exception() throw inside the Try/catch block and also put out the if() conditional. See:

function cadastro($valor){
      $sql = mysqli_query($conexao, "SELECT * FROM tabela WHERE campo = '".$valor."'");

        if(strlen($valor) < 3){
        $erro = "O sabor não pode ser inferior a 3";
        }else{
        $sqlCadastrar = mysqli_query($conexao,"INSERT INTO tabela VALUES(null,'".$valor."');");            

        }
 try{
      if($erro == true){
    throw new Exception($erro);      
    }else if(mysqli_affected_rows($conexao) > 0){
               return "<script>window.location.href='cadastrar.php';</script>"; 
               }
             } catch (Exception $erro) {
                 return $erro->getMessage();
        }  
}
  • Are you simulating an error, so error successfully made right? or was it an invalid success?

  • 2

    I think the test you’re trying to do requires that you throw the exception inside the block try.

  • It was an invalid success. If the taste value is less than 3 characters, it would give an error.

1 answer

2


This message is displayed on the screen because your Exception has not been handled, so a fatal error will be triggered to warn you as the documentation explains.

If an Exception is not Caught, a PHP Fatal Error will be issued with an "Uncaught Exception ..." message, unless a Handler has been defined with set_exception_handler().

This (strange) example code, simulates the problem, catch expects a DogeScareException by a Exception which is different from the type expected for treatment was released and was not captured.

try{
    throw new Exception("WoOoOoW Exception", 2015);
}catch(DogeScareException $e){
     echo 'Caught exception#';
}   

Another reason is the comment from Marco Aurélio Deleu, the throw new Exception(...) must be inside a block try-catch from the account the Exception will never be treated and literally explodes on screen.

  • Right. I did as Marcus Aurelius said, but right now calling the function, no error appears.

  • 2

    Such Exception. Very Scary. Vote up. Wow..

  • 1

    @Jose.Marcos can edit the question with the full code of the function if it is not too long?

  • Colleagues, I did it. I put Try and catch out of parole if(). Without you I would not have made it. I adjusted the correction in the edition. Thank you all, but I will have to choose an answer ;)

  • 1

    @Mark, now I understand the error, your code was in if(strlen($valor) < 3) however the catch was inside the else. Be advised that strlen() does not return the number of characters but bytes, do a simple test(echo strlen('ação');. When your code falls on else $erro will not exist for comparison if($erro == true). PHP cast all the time so you don’t have to if($var == true) just enough if($var)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.