breakingBad
breakingBad

Reputation: 5

Exception handling in this scenario

Trying my hands on Java for the first time, please be kind. I have following code in a Web Controller where a service is called based on enclosed Switch-Case statement.
Issue I am facing is, if the service call throws an Exception, this exception gets shown on the JSP page. Basically the code never reaches the lines:

if(!statusFlag)
{
      model.addAttribute("statusFlag", statusFlag);
      return "myJspPage"
}

How do I make sure the executing goes to above lines, even though an exception is thrown in the WebService call at:

statusFlag = myWebService.getMeStatus();

Should I enclose the whole Switch Statement inside try-catch block?

Snippet:

@Controller
public String mySpringController() throws Exception
{
   //rest of the controller code
        switch ( condition )
        {
        case MAY :
                   statusFlag = myWebService.getMeStatus();

                   if(!statusFlag)
                   {
                      model.addAttribute("statusFlag", statusFlag);

                      return "myJspPage"
                   }
                   break;

        case JUNE :
                   statusFlag = myWebService.getMeStatus();

                   if(!statusFlag)
                   {
                      model.addAttribute("statusFlag", statusFlag);

                      return "myJspPage"
                   }
                   break;

        case JULY :
                   statusFlag = myWebService.getMeStatus();

                   if(!statusFlag)
                   {
                      model.addAttribute("statusFlag", statusFlag);

                      return "myJspPage"
                   }
                   break;
        default:

                  //Do something by default.

        }

return "myJspPage";

}

Upvotes: 0

Views: 130

Answers (3)

jbx
jbx

Reputation: 22148

If that line is throwing an Exception it means that it is never returning, so statusFlag is still with its original value and the execution of that method has stopped. You need to surround it in a try - catch if you want to catch the Exception and do something about it.

I see you are using Spring. In Spring Controllers you can also have your own special methods which get invoked when an Exception occurs. Using the @ExceptionHandler annotation.

Upvotes: 1

You're writing Java in the style of C++, where you're returning error codes and then checking them to determine whether anything went wrong. There are a number of issues with this snippet, but the reason for the exception display is that you never catch the exception that's being thrown. Where you should put your try-catch block depends on what the exception means; if it's something that isn't specific to a particular month, then yes, enclose the entire switch statement to share the error handling.

As an aside, did you copy and paste your actual code, or did you try to retype an example? Those case blocks all look identical.

Upvotes: 0

75inchpianist
75inchpianist

Reputation: 4102

I don't see the purpose of your switch statement since each case does the same thing.

But basically you can put your webservice call in a try-catch block

try{
    webservice.call();
}
catch (Exception e){
   // handle the exception
}
finally{
  //anything in here will be executed regardless if an exception is caught or not
}

Upvotes: 0

Related Questions