简体   繁体   中英

java - Exception not getting caught

I am using Spring ROO. In my web application I can create many users and save. I can update the existing users as well.

For the update scenario we are using merge() method to update the existing data. In database, the column 'username' is unique. Following is the scenario.

  1. The user create an user name 'Sean' with mobile number '6039274849'

  2. The user create another user named 'Parker' with mobile number '8094563454'

  3. When the user tries to update the second user 'Parker' with 'Sean', I am getting exception.

In the stacktrace I could see the following exception being the causes

  1. caused by ConstraintviolationException
  2. caused by SQLException
  3. caused by TransactionSystemException
  4. caused by PersistenceException
  5. caused by TransactionRollbackException

I tried the do the following

public String merge()
  {
     try{
          //code to merge
        }
     catch(????? e){
         throw e;
     }
  }

I tried to add the above 5 exceptions in '????' . But I couldnot catch still.

Can anyone please tell which exception I need to add in '????' to catch the exception from the above list?

PS: I am using Spring ROO. So I am changing code in .aj file. Please dont close this question as duplicate. I am expecting an answer from anyone for my issue before closing this question.

As a last resort, you can just catch the all-purpose exception

public String merge()
{
     try{
          //code to merge
        }
     catch(Exception e){
         //handle e here.
     }
}

Um, aren't you just rethrowing the exception in your catch? It should be the "most-recent" exception in the trace, so ConstraintValidationException .

Note that typically in Spring/Hibernate apps, the exception bubbling out of your code is what causes transactions to roll back. If you catch the exception, you will probably prevent that, which might lead to data inconsistencies.

When in doubt I try catching a Throwable and either add break point or log it out to see exactly what it is. Then change code accrodingly.

I had the same probelem lately. It seems that spring wrap exception in it's own exception class.
I solved this problem with:

try {
   ...
}
catch(Exception e){
  System.out.println(e.getClass().getName());
}

with that you will discover what exception has been realy thrown;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM