Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:48:15+00:00 2026-06-12T19:48:15+00:00

I have the following code in the Controller class and for some reason although

  • 0

I have the following code in the Controller class and for some reason although i have exception handling implemented as well as a try…..catch block i am still unable to capture my exceptions.

I am just executing a test, in the DAO class i change the sql string that inserts into the database to leave out a column just so the DAO will fail. The DAO class fails and the error is written to the logs however even thou the officerManager.RegisterOfficer(officer) was not successful the code goes on to return the model.addAttribute(“results”,”Record Was Saved”).

This is not accurate and i would like for the controller to throw an error. Under is the code.

Controller

@RequestMapping(value="officer_registration.htm", method=RequestMethod.POST)
public ModelAndView handleRequest(@Valid @ModelAttribute Officers officer,BindingResult result,ModelMap m,Model model,HttpServletRequest request,HttpServletResponse response)throws Exception{

         try{
             if(result.hasErrors()){

                 model.addAttribute("division", myDivision);
                 model.addAttribute("position", myPosition);
                 model.addAttribute("gender", myGender);
                 return new ModelAndView("officer_registration");

            }else{

                //check the request if its an update or an insert
                String user_request = request.getParameter("user_request");
                logger.info("The Users Request Was " + user_request);

                if (user_request.equals("Save")){

                        officerManager.RegisterOfficer(officer);
                        model.addAttribute("results","Record Was Saved");

                }else{

                    officerManager.UpdateOfficer(officer);
                    model.addAttribute("results","Record Was Updated");
                }

                 model.addAttribute("division", myDivision);
                 model.addAttribute("position", myPosition);
                 model.addAttribute("gender", myGender);            
                return new ModelAndView("officer_registration");
            }   


         }catch(Exception e ){
             model.addAttribute("division", myDivision);
             model.addAttribute("position", myPosition);
             model.addAttribute("gender", myGender);
             model.addAttribute("results","Error: Unable to Save Record!");
             return new ModelAndView("officer_registration");
         }



     }

DAO

public void saveOfficer(Officers officer) {
    logger.info("In saveOfficer");


    //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    try{

        int count = getJdbcTemplate().update("INSERT INTO crimetrack.tblofficers (userName,password, fName, lName, oName, divisionNo, positionId, emailAdd, startDate, endDate, genderId,phoneNo, dob,badgeNo) "+
                                            "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
                                            , new Object[]{officer.getUserName(),StringSecurity.EncryptString(officer.getPassword()),officer.getfName(),
                                             officer.getlName(),officer.getoName(),officer.getDivisionNo(),officer.getPositionId(),
                                             officer.getEmailAdd(),officer.getStartDate(),officer.getEndDate(),officer.getGenderId(),
                                             officer.getPhoneNo(),officer.getDob(),officer.getBadgeNo()});

    logger.info(count +" Rows affected in tblOfficers");



    }catch(Exception e){

        logger.error("Could not save officer ", e);
    }       
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:48:16+00:00Added an answer on June 12, 2026 at 7:48 pm

    You’re not allowing the error to bubble up back to the controller.

    You’re handling the exception within the DAO, in which case the method exits normally, and no exception is caught within the Controller.

    Either don’t surround the DAO with a try catch and let the exception bubble back to the controller (recommended), or catch and rethrow the exception (if you follow this route, throw as a RuntimeException, either create your own, or rethrow as a RuntimeException, that way you won’t have to catch all the way through the call stack.)

    Also, it’s generally frowned upon to catch generic exception as it is tougher to nail down exactly what caused it unless you look within the logs. Knowing which exceptions to handle ahead of time is usually better practice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller class as in the following code segment: @Controller public class
I have the following code public class BaseController : Controller { SqlConnection con; DataSet
I have the following code right after my controller class declaration before_filter :load_form, :except
I have the following code in my controller action method: if (User.Identity.IsAuthenticated) { //
In Symfony 2 I have the following code in my Controller: // prepare to
I have the following code in my gsp <select onChange=${remoteFunction(action:'superior', controller:'employee', params: '\'name=\' +
I am using Codeigniter message library. In my controller I have the following code
I have a simple controller test, containing a.o. the following code: context POST :create
I have a controller with the following action code: public JsonResult CheckPasswordStrength(string password, string
I have the following View, Controller and Comment.cs code, but I get an error,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.