I have a question about how to apply try
and catch
to 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();
}
}
Calling the method is done 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);
}
Is the correct way to try
and catch
be applied?