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)
	@ResponseBody
	public void getProgressNotes(HttpServletRequest request,
			HttpServletResponse response, @RequestParam Long noteId) throws PMRUserException {
		byte[] byteArray = iprogressnotesService.getProgressNotes(noteId); 
		OutputStream respOut = null;
		try{
		     response.setContentType("application/pdf");
		     response.setContentLength(byteArray.length );
		     respOut = response.getOutputStream(); 
		    respOut.write(byteArray);
		    respOut.flush();
		    respOut.close();

		} catch (Exception e) {
			throw new CustomUserException(e.getMessage(),e);
		}finally{
			try {
				if(respOut!=null){
					respOut.close();
				}
			} catch (IOException e) {
				throw new CustomUserException(e.getMessage(),e);   //ERROR THROWN HERE
			}
		}
		
	}

Open in new window

onaled777Asked:
Who is Participating?
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.

krakatoaCommented:
Why don't you just close the stream once in the finally block?
0
dpearsonCommented:
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 ){
      respOut.close();
}

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:
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
so Java handles the cleanup for you.

Doug
1

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
krakatoaCommented:
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.
0
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
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.