简体   繁体   English

java - 异常没有被抓住

[英]java - Exception not getting caught

I am using Spring ROO. 我正在使用Spring ROO。 In my web application I can create many users and save. 在我的Web应用程序中,我可以创建许多用户并保存。 I can update the existing users as well. 我也可以更新现有用户。

For the update scenario we are using merge() method to update the existing data. 对于更新方案,我们使用merge()方法来更新现有数据。 In database, the column 'username' is unique. 在数据库中,“用户名”列是唯一的。 Following is the scenario. 以下是该方案。

  1. The user create an user name 'Sean' with mobile number '6039274849' 用户使用手机号码“6039274849”创建用户名“Sean”

  2. The user create another user named 'Parker' with mobile number '8094563454' 用户创建另一个名为“Parker”的用户,手机号码为“8094563454”

  3. When the user tries to update the second user 'Parker' with 'Sean', I am getting exception. 当用户尝试用'Sean'更新第二个用户'Parker'时,我会遇到异常。

In the stacktrace I could see the following exception being the causes 在堆栈跟踪中,我可以看到以下异常是原因

  1. caused by ConstraintviolationException 由ConstraintviolationException引起
  2. caused by SQLException 由SQLException引起
  3. caused by TransactionSystemException 由TransactionSystemException引起
  4. caused by PersistenceException 由PersistenceException引起
  5. caused by TransactionRollbackException 由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 '????' 我试图在'????'中添加上述5个例外 . 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. PS:我正在使用Spring ROO。 So I am changing code in .aj file. 所以我在更改.aj文件中的代码。 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 作为最后的手段,您可以捕获通用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 . 它应该是跟踪中的“最新”异常,因此ConstraintValidationException

Note that typically in Spring/Hibernate apps, the exception bubbling out of your code is what causes transactions to roll back. 请注意,通常在Spring / Hibernate应用程序中,冒出代码的异常是导致事务回滚的原因。 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. 如果有疑问,我会尝试捕捉一个Throwable并添加断点或注销它以确切地看到它是什么。 Then change code accrodingly. 然后累积更改代码。

I had the same probelem lately. 我最近有同样的问题。 It seems that spring wrap exception in it's own exception class. 它似乎是自己的异常类中的spring换行异常。
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; 有了它,你会发现什么异常被抛出;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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