This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Preserve Stack trace - Sonar violation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Preserve Stack trace - Sonar violation" Watch "Preserve Stack trace - Sonar violation" New topic
Author

Preserve Stack trace - Sonar violation

kark Kumr
Greenhorn

Joined: Sep 21, 2012
Posts: 5
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

Joined: Oct 13, 2005
Posts: 37884
    
  22
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

Joined: Sep 21, 2012
Posts: 5
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

Joined: Oct 13, 2005
Posts: 37884
    
  22
You’re welcome
Don’t know about SONAR. Anybody else?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

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

Joined: Mar 05, 2008
Posts: 2982
    
    9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Preserve Stack trace - Sonar violation
 
Similar Threads
Doubt in Rule Roundup Answer: catch block Exception Order
Exception stack trace in logger
getting PrintStream or PrintWriter from logger
exception stack trace not going to log file
Logging Throwables Java 5