1
I have a question about how to apply the try
and catch
in a method.
public function cadastrarUsuarios(parametros){
mysqli_query($this->conexao,"INSERT....");
// Aqui que vem a dúvida
try{
if(mysqli_affected_rows($this->conexao) > 0){
$_SESSION["Sucesso"] = time() + 3;
return "<script>window.location.href='pagina.php';</script>";
}else{
$_SESSION["Erro"] = time() + 3;
$erro = '';
throw new Exception($erro);
}
}catch (Exception $erro){
return $erro->getMessage();
}
}
The method call is made on another page and at the top:
include("classes/metodosClass.php");
$metodos = new metodosClass();
if(filter_input(INPUT_POST, "Submit") == "Cadastrar"){
....
echo $metodos->cadastrarUsuarios(parametros);
}
It is correct how the try
and catch
?
That answer might help you What Try/Catch Blocks are for and when they should be used?
– DNick