Will there be any negative performance implications on the following code due to methods that do not throw exceptions being inside of a try block?
String user;
try {
user = SomeHelper.getUserName(); //Does not throw anything explicitly
if (SomeHelper.isSomething(user)) { //Does not throw anything explicitly
user.doSomeSafeThing(this); //Does not throw anything explicitly
}
else {
user.doOtherSafeThing(this); //Does not throw anything explicitly
}
SomeOtherHelper.methodThatCanBlow(User.getCredentials(context)); //This will throw exception
} catch (Exception e) {
e.printStackTrace();
}
I believe that the compiler will not refactor any of this code due to the fact that any of those methods could potentially throw a NullPointerException, or another RuntimeException and actually be caught in the catch block.
So I beg to ask the question, would the following code be more efficient?
String user;
user = SomeHelper.getUserName(); //Does not throw anything explicitly
if (SomeHelper.isSomething(user)) { //Does not throw anything explicitly
user.doSomeSafeThing(this); //Does not throw anything explicitly
}
else {
user.doOtherSafeThing(this); //Does not throw anything explicitly
}
try {
SomeOtherHelper.methodThatCanBlow(User.getCredentials(context)); //This will throw exception
} catch (Exception e) {
e.printStackTrace();
}
Will there be any negative performance implications on the following code due to methods that do not throw exceptions being inside of a try block?
No, it won't have any performance implications, but:
try
block anywayException
, instead catching specific exceptionsSee more on this question at Stackoverflow