2
I am making a web application in java and I have some doubts regarding the block Try catch, I am using the standard MVC 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 the 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 wonder if in case of any problem in the Model, how I put it to return null
, he will not enter the catch of Controller
? I must put the try/catch
in all files or only in Controllers
?
I have two questions to answer your answer : http://answall.com/questions/58536/blocos-try-catch?rq=1 and http://answall.com/questions/108484/uso-espec%C3%Adfico-do-Try-catch? Rq=1
– Igor Contini
Did you create your own MVC? Or is it some specific framework?
– Guilherme Nascimento