Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

P.S: 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.

like image 259
user1514499 Avatar asked Jul 13 '26 02:07

user1514499


2 Answers

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.
     }
}
like image 95
SomeKittens Avatar answered Jul 14 '26 17:07

SomeKittens


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.

like image 21
hvgotcodes Avatar answered Jul 14 '26 15:07

hvgotcodes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!