Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Preserve Stack trace - Sonar violation

 
kark Kumr
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am new to java and I have given to fix these violations. Can any one let me the fix for this Preserve stack trace. I should be passing the original exception but in this code, how to fix it. Thanks for your time

 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have removed the excess spaces and line numbers from your code; both make it harder to read. Please explain the problem a bit more.
 
kark Kumr
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell for purging the spaces. At line 17, my SONAR compiler complains "Preserve Stack trace violation". Generally, for other types of code for Preserve stack trace, I have passed the exception cause in the constructor. But here, we got two catch blocks, if inner catch block executes, then possibly original stack trace might be lost from first catch block.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
Don’t know about SONAR. Anybody else?
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original exception is e.

If the rollback fails the exception is now transEx.

In this case you throw the transEx but lose the original Exception e.

You need to chain the exceptions together in that case.

WP
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, chaining both exceptions to a new exception (or to each other) could be tough. You could try it with initCause(), but depending on how the original exception was created, that may not work.

I tend to think that Sonar/Findbugs are full of crap here. You've logged each exception; that should be enough as far as preserving information is concerned. If you also want to throw a new exception and include the cause, great - but personally I would consider the original exception more important than the subsequent exception. That's your call though. Either way, FindBugs is simply wrong here. So the best solution is to disable it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic