I am doing a web application in java and I have some doubts regarding the try catch block, I am using the MVC standard and I have the following codes:
Controller:
try {
String pesquisa = "%" + request.getParameter("pesquisa") + "%";
List<Administrador> adm = serviceAdm.listarAdm(pesquisa);
request.setAttribute("adm", adm);
RequestDispatcher disp = request.getRequestDispatcher("administradores.jsp");
disp.forward(request, response);
} catch (Exception ex) {
System.out.println("Erro: " + ex);
request.setAttribute("erro", true);
RequestDispatcher disp = request.getRequestDispatcher("principal.jsp");
disp.forward(request, response);
}
Model:
public List<Administrador> listarAdm(String pesquisa) {
try {
return (List<Administrador>) admDB.selectAdms(pesquisa);
} catch (Exception ex) {
return null;
}
}
And I use DAO to make the connection:
public List<Administrador> selectAdms(String pesquisa) {
List<Administrador> usuarios = manager
.createQuery("select a from Administrador a where nome LIKE :pesquisa")
.setParameter("pesquisa", pesquisa).getResultList();
return usuarios;
}
I would like to know if I have a problem with Model, as I put it to return null
, it will not enter the Controller
catch? Should I put try/catch
in all files or only Controllers
?