Reusing Exceptions

I have a caching tool for recieving/holding Objects.

It gets used by calling for an Object with params that define where/how to get/make the Object.

Once I get the Object I save it, so that if it is asked for again, it is not needed to create/get a new one.

BUT

for some params the Object asked for isn't there or able to be made and an Exception is needed to be returned.

Can I put this Exception in my cache, so I can just return that instead of going thru the code to make/get my Object, when I know it will fail.

Can you reuse Exceptions?

Are they changed in anyway by the bit that catches them?

The exception I am after saving, in this case, is a MissingResourceException but I suppose that doesn't really matter, it'll eithor be OK or not.

[803 byte] By [simon_orangea] at [2007-10-1 20:02:56]
# 1

You can create, keep and reuse Exceptions just as any other Java object.

The Exception is not usually changed in the catch section of code, but you could of course write code that changes the Exception or throws a completely new one, etc.

If you have a need to make the Exception available to a piece of code (perhaps in a different thread) other than the one with the try... catch then there is no inherent problem in passing it as a method argument or, as you suggest, placing it in your cache.

Chris.

ChrisBoya at 2007-7-11 16:35:19 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...