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?
– rray
I think the test you’re trying to do requires that you throw the exception inside the block
try
.– Marco Aurélio Deleu
It was an invalid success. If the taste value is less than 3 characters, it would give an error.
– user24136