2

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)

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180

4 Answers4

5

If you have Java 7 or later and would like to remain specific about the exceptions that you catch, you could use ReflectiveOperationException, the base class of ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException, and NoSuchMethodException:

try {
    ...
} catch (ReflectiveOperationException e) {
    e.printStackTrace();
} catch (SecurityException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
}

Catching all exceptions would change the semantics of your program.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
5

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) {
    // ...
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • so I better do nothing here? – Kaloyan Roussev Oct 21 '14 at 16:55
  • You could also explicitly catch-and-rethrow `RuntimeException`s. – yshavit Oct 21 '14 at 16:57
  • @J.K.: I'm not sure what you mean by "do nothing". – Jon Skeet Oct 21 '14 at 16:57
  • By do nothing I mean not change the method, but leave it as is – Kaloyan Roussev Oct 21 '14 at 16:57
  • @yshavit: But then that would come ahead of `Exception`, so `IllegalArgumentException` (for example) would be rethrown too... – Jon Skeet Oct 21 '14 at 16:58
  • @J.K.: Well we don't know what version of Java you're using (although I assume this is Android, which I *think* supports multi-catch), or what you want to achieve. I've stated the behaviour, and a way of performing the same action for a specific set of exceptions. It's up to you to work out how to use that information. – Jon Skeet Oct 21 '14 at 16:59
  • What if I use Java 7+ but another developer doesnt? Isnt it better to stick with what works with all Java versions – Kaloyan Roussev Oct 21 '14 at 17:01
  • @J.K. : That's a very limiting approach. It's not like Java 7 is brand new or anything. But we don't know which other developers are likely to consume your code, as we don't have much context. – Jon Skeet Oct 21 '14 at 17:03
1

Yes it will catch all Exception and subclass of Exception. One of the reason where you would catch specific exceptions is you have specific actions to perform incase of exception 1 then you catch that exception.

SMA
  • 36,381
  • 8
  • 49
  • 73
1

Yes that will work. The reason is because Exception is the base class of all exceptions, in other words every other exception is a subclass of Exception, so they will be caught by catch(Exception e).

See: How can I catch all the exceptions that will be thrown through reading and writing a file?

Community
  • 1
  • 1
user3270760
  • 1,444
  • 5
  • 23
  • 45