0

I have some problems making the exception do that i want!. I have created a servlet, which i want to handle all exceptions. My class building for this scenario that i need help too you see under here:

Servlet: This exception handling is only for 1 method,

try {
completed = func.addNewOperator(userId, name, ini, cpr, password, role);
}catch (Exception e) {
    Error = "There was a problem with database access";
    response.sendRedirect("SystemError.jsp?Error_resultat=" + Error);
} catch (IOException e) {
    Error = "Error found with connection";
    response.sendRedirect("SystemError.jsp?Error_resultat=" + Error);
} catch (RuntimeException e) {
    Error = "Error found with entered values";
    response.sendRedirect("SystemError.jsp?Error_resultat=" + Error);
} catch (Exception e) {
    Error = "Serious error found!";
    response.sendRedirect("SystemError.jsp?Error_resultat=" + Error);

The func.addNewOperator is called in my function class:

Function:

A small example from my function method you see under here:

public boolean addNewOperator(String userId, String name, String ini, String cpr, String password, String role ) throws Exception { 

int id = Integer.parseInt(userId);

}

So lets say if it can't parse it to and integer, it should throws the exception to my servlet, but this is not working. I want to throw the exception to the servlet, and then the servlet should send a response to open SystemError.jsp with an error message as parameter.

Can anyone see if i forgot something??

Pixel
  • 15
  • 2
  • 6

2 Answers2

0

The order is whatever matches first, get's executed (as the JLS clearly explains).

If the first catch matches the exception, it executes, if it doesn't, the next one is tried and on and on until one is matched or none are.

So, when catching exceptions you want to always catch the most specific first and then the most generic (as RuntimeException or Exception).

Read more Order of catching exceptions in Java

catch (Exception e) should be last catch block in the chain.

It's worth reading Oracle Java Tutorial - Exceptions

Try in this way and don't forget to log the exception in log file.

try {
    completed = func.addNewOperator(userId, name, ini, cpr, password, role);
}catch (SQLException e) {
    Error = "There was a problem with database access";     
} catch (IOException e) {
    Error = "Error found with connection";
} catch (RuntimeException e) {
    Error = "Error found with entered values";
} catch (Exception e) {
    Error = "Serious error found!";
}

if(Error != null){
    response.sendRedirect("SystemError.jsp?Error_resultat=" + Error);
}

NumberFormatException is a RuntimeException.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
0

First, you should never redirect to a jsp page, but forward to it. Normally, jsp files are placed under WEB-INF and are never called directly but allways through server. Here is one example of forwarding (derived from from Java Tips - How to forward requests from Servlet to JSP :) :

request.setAttribute("error", error);
getServletConfig().getServletContext().getRequestDispatcher("/WEB-INF/SystemError.jsp")
    .forward(request,response);

and your get error (first letter lowercased because it's a variable) in your jsp via ${error}

And anyway, you shouldn't deal with that at your servlet level but delegates this low level job to the container as explained in that other post from SO : How to Properly Handle Exceptions in a JSP/Servlet App?.

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • What about html files, should i also put them under WEB-INF? – Pixel Jun 14 '14 at 13:56
  • Of course not, as they will be directly asked from client browser. HTML, CSS, or images must be out of WEB-INF for the controller to serve them directly. But jsp are source elements (they will be translated to HTML) and should be hidden and accessed only via forward from a servlet. – Serge Ballesta Jun 14 '14 at 14:08
  • well worked with redirect, but now i get this error: java.lang.IllegalStateException: getOutputStream() – Pixel Jun 14 '14 at 14:10