I am trying to load an image in memory but might have memory issues since i have some other images loaded. These images have a "visible" field that dictates whether they are visible or not. Regardless of the visibility i keep them in memory for fast loading (when they become visible again).
But since i have many of them in memory i want to try to load a new image and if i run into memory issues, release the non-visible images and try again. Now so far i am using this rather ugly (and wrong for some reason, i am sure) piece of code :
try {
image = GraphicsUtilities.loadImage(filePath);
} catch (OutOfMemoryError e) {
removeHiddenImageReferences();
try {
image = GraphicsUtilities.loadImage(filePath);
} catch (OutOfMemoryError ee) {
ee.printStackTrace();
JOptionPane.showMessageDialog(parent,
"There is not enought memory to load this image",
"Not enough memory", JOptionPane.WARNING_MESSAGE);
}
}
My question is, how should i handle this kind of case? I feel that catching an exception and re catching an exception inside the catch clause is bad.