Question

I recently modified the code below to catch the Exception from the DB and in case of error show the Error message on the GUI......However my supervisor said its a bad idea!! Well to me it seems more convinient and couldnt think of any possible reason as to why is this a bad from design perspective>????

Thanks in advance!!

try{
getFromDB(){
//Getting some info from the Database eg Date, lists of pack etc}
}
 catch (SQLException e) {
        //C11821735 
        req.setAttribute("dateDebut", dateDebut);
        req.setAttribute("dateFin", dateFin);
        req.setAttribute("Erreur", "Erreur : " + e.getMessage());
        req.setAttribute("Pack", packList);
        req.setAttribute("LISTE", liste);
        return mapping.findForward("self");
    }

This piece of code serves my purpose of throwing error to the user if for ex. there is any primary key violation and again redirecting it to the same page.....i just want to know what are the cons of using this approach.....is it a really bad design or in which cases it could fail?????

Was it helpful?

Solution

I'm not clear what the code in your exception handler actually does. Setting values in the request? But I'm guessing that indeed it does result in error messages in the GUI.

As you say, if you need to catch an exception and display an error I'm not sure what else you can do. We might ask whether there are other exceptions you should be catching, but I don't see an intrinsic problem here.

There are general code style rules about not using exception to manage "normal" code paths, but here you are using a utility function that does throw an exception in exceptional cases and need to handle that exception.

In your situation I would ask my leader for an example of what he would prefer. Two possibilities: we learn something or he goes "oh yes, you do need to do that". Admittedly the thing we might learn is that he prefers stuff we don't like, but that's life.

OTHER TIPS

Exceptions should only be used for "exceptional" cases. Otherwise code will be hard to read, likely inefficient etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top