Potential Resource Leak Error Being Thrown

The code below throws the error 'Potential resource leak. respOut may not be closed at this location ' on the line indicated.

Can you help me figure out why?

	@RequestMapping(value = "/getProgressNotes.htm", method = RequestMethod.GET)
	public void getProgressNotes(HttpServletRequest request,
			HttpServletResponse response, @RequestParam Long noteId) throws PMRUserException {
		byte[] byteArray = iprogressnotesService.getProgressNotes(noteId); 
		OutputStream respOut = null;
		     response.setContentLength(byteArray.length );
		     respOut = response.getOutputStream(); 

		} catch (Exception e) {
			throw new CustomUserException(e.getMessage(),e);
			try {
			} catch (IOException e) {
				throw new CustomUserException(e.getMessage(),e);   //ERROR THROWN HERE

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Why don't you just close the stream once in the finally block?
I believe this is an Eclipse warning?

If so I think it's just reporting the warning incorrectly - it's probably seeing this structure:
if( condition ){

So to it, the close call may not happen.
It's just to us, we can see that in that case, respOut is null and so there's no need for it to be closed.

So this is likely just a problem with Eclipse.

How to fix it?  If you're using Java 7 the best approach is probably to rewrite the code to use the "try-with-resource" pattern:
so Java handles the cleanup for you.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It could be that the call is being made to the AutoCloseable interface rather than Closeable, and when this is done more than once, I thought I understood that it can have a peculiar effect.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.