From: laredotornado on 27 Jan 2010 17:55 Hi, I'm using Java 1.5 on a Resin 3.0.19 web/app server. I'm using PMD as the code-checker, and in an EJB, it is complaining about public List<SubvendorCustomerRecord> getSubvendorCustomerReport( final Integer subvendorId) throws RemoteException { List<SubvendorCustomerRecord> results = new ArrayList<SubvendorCustomerRecord>(); try { AbstractReport report = new SubvendorCustomerReport(subvendorId); results.addAll( report.getReportData() ); } catch (Exception e) { LOG.error(e.getMessage(), e); throw new RemoteException(e.getMessage()); } return results; } Specifically the "throw new RemoteException(e.getMessage());" line, with the complaint, "New exception is thrown in catch block. Original stack trace may be lost." Does anyone know how I can rewrite the above so that I can still log any error but at the same time get rid of the annoying code-checker warning? Thanks, - Dave
From: Peter Duniho on 27 Jan 2010 18:07 laredotornado wrote: > [...] > Specifically the "throw new RemoteException(e.getMessage());" line, > with the complaint, "New exception is thrown in catch block. Original > stack trace may be lost." Does anyone know how I can rewrite the > above so that I can still log any error but at the same time get rid > of the annoying code-checker warning? What happens if you use the overload for the RemoteException() constructor that takes a Throwable argument? That is: throw new RemoteException(e.getMessage(), e); For that matter, why aren't you specializing the message? What's the point of converting from the original exception to a new type if you don't have anything more to say? Pete
From: Steven Simpson on 27 Jan 2010 18:14 On 27/01/10 22:55, laredotornado wrote: > } catch (Exception e) { > LOG.error(e.getMessage(), e); > throw new RemoteException(e.getMessage()); > } > > Specifically the "throw new RemoteException(e.getMessage());" line, > with the complaint, "New exception is thrown in catch block. Original > stack trace may be lost." Does anyone know how I can rewrite the > above so that I can still log any error but at the same time get rid > of the annoying code-checker warning? > Make the RemoteException wrap the Exception, I guess?: new RemoteException(e.getMessage(), e); -- ss at comp dot lancs dot ac dot uk
From: Lew on 27 Jan 2010 19:12 laredotornado wrote: >> } catch (Exception e) { >> LOG.error(e.getMessage(), e); >> throw new RemoteException(e.getMessage()); >> } >> > >> Specifically the "throw new RemoteException(e.getMessage());" line, >> with the complaint, "New exception is thrown in catch block. Original >> stack trace may be lost." Does anyone know how I can rewrite the >> above so that I can still log any error but at the same time get rid >> of the annoying code-checker warning? Other exceptions (not 'java.rmi.RemoteException' or some others) have a constructor that takes just a 'Throwable cause' as an argument, the result of which, according to <http://java.sun.com/javase/6/docs/api/java/lang/Exception.html> is: > Constructs a new exception with the specified cause and a detail message of > (cause==null ? null : cause.toString()) Side notes: - It is usually a bad idea to catch 'Exception'. You should catch specific checked exceptions. - 'LOG' does not follow the Java coding conventions: <http://java.sun.com/docs/codeconv/index.html> - Do not use TAB characters to indent Usenet code samples! Use a maximum of four *spaces* to indent per level. laredotornado, you've been around this group long enough to know better. -- Lew
From: Daniel Pitts on 27 Jan 2010 19:51
On 1/27/2010 4:12 PM, Lew wrote: > laredotornado wrote: >>> } catch (Exception e) { >>> LOG.error(e.getMessage(), e); >>> throw new RemoteException(e.getMessage()); >>> } > Side notes: > - 'LOG' does not follow the Java coding conventions: > <http://java.sun.com/docs/codeconv/index.html> I'm curios about this.. private static final Logger LOG = ...; It is private, static, and final: Which convention would you use? -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/> |