Sunday 15 February 2015

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



java - 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 method found on internet:

private static int getexiforientation(string src) throws ioexception { int orientation = 1; seek { /* * if targeting 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 }); } } grab (classnotfoundexception e) { e.printstacktrace(); } grab (securityexception e) { e.printstacktrace(); } grab (nosuchmethodexception e) { e.printstacktrace(); } grab (illegalargumentexception e) { e.printstacktrace(); } grab (instantiationexception e) { e.printstacktrace(); } grab (illegalaccessexception e) { e.printstacktrace(); } grab (invocationtargetexception e) { e.printstacktrace(); } grab (nosuchfieldexception e) { e.printstacktrace(); } homecoming orientation; } // end of getexiforientation

can replace these multiple grab statements just

} grab (exception e) {

or there chance if dont mention each exception name, might slip grab exception e check ?

to sum up: "catch exception e" grab kinds of exceptions or should each 1 named individually (all in cases not want react differently in each case)

to sum up: "catch exception e" grab kinds of exceptions or should each 1 named individually

it catches of type exception or subclass. not grab other throwables, e.g. error. given exceptions you've specified do subclass exception, can grab that.

however, it's still going alter behaviour - because also grab runtimeexceptions, ones weren't mentioned before. if you're using java 7 or higher, might want utilize the ability specify multiple types single grab block:

catch (classnotfoundexception | securityexception | nosuchmethodexception | illegalargumentexception | instantiationexception | illegalaccessexception | invocationtargetexception | nosuchfieldexception e) { // ... }

java

No comments:

Post a Comment