Colleagues. I set the code. The solution was based on colleagues putting the throw new Exception () inside the try / catch block and also placed outside 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();
}
}