I am borrowing a method that catches 10 kinds of exceptions but does nothing with them. Can I replace them with just (Exception e)

Im borrowing this method I found on the internet:

private static int getExifOrientation(String src) throws IOException {
    int orientation = 1;
    try {
        /*
         * if you are targeting only api level >= 5 ExifInterface exif = new ExifInterface(src); orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
         */
        if (Build.VERSION.SDK_INT >= 5) {
            Class<?> exifClass = Class.forName("android.media.ExifInterface");
            Constructor<?> exifConstructor = exifClass.getConstructor(new Class[] { String.class });
            Object exifInstance = exifConstructor.newInstance(new Object[] { src });
            Method getAttributeInt = exifClass.getMethod("getAttributeInt", new Class[] { String.class, int.class });
            Field tagOrientationField = exifClass.getField("TAG_ORIENTATION");
            String tagOrientation = (String) tagOrientationField.get(null);
            orientation = (Integer) getAttributeInt.invoke(exifInstance, new Object[] { tagOrientation, 1 });
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    return orientation;
} // End of getExifOrientation

Can I replace these multiple CATCH statements with just

} catch (Exception e) {

or is there a chance that if I dont mention each exception by its name, it might slip by the catch Exception e check ?

To sum up: does "catch Exception e" catch all kinds of exceptions or should each one be named individually (all this in cases where we do not want to react differently in each case)

Jon Skeet
people
quotationmark

To sum up: does "catch Exception e" catch all kinds of exceptions or should each one be named individually

It catches everything of type Exception or a subclass. It will not catch other Throwables, e.g. Error. But given that all the exceptions you've specified do subclass Exception, you can catch that.

However, it's still going to change behaviour - because it will now also catch all RuntimeExceptions, even ones that weren't mentioned before. If you're using Java 7 or higher, you might want to use the ability to specify multiple types for a single catch block:

catch (ClassNotFoundException | SecurityException |
       NoSuchMethodException  | IllegalArgumentException |
       InstantiationException | IllegalAccessException |
       InvocationTargetException | NoSuchFieldException e) {
    // ...
}

people

See more on this question at Stackoverflow