• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Preserve Stack trace - Sonar violation

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
Don’t know about SONAR. Anybody else?
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 4327
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic