I got tired of adding seemingly endless if-else statements into my code, so I wondered if it would be better practice if I'd just catch Exceptions if something wasn't right. Eg. instead of saying:
public static boolean firstStringOfArrayIsTheSameAsTheWord(String word, String[] array) {
if(word != null) {
if(array.length > 0) {
return word.equalsIgnoreCase(array[0]);
}
}
return false;
}
I'd just have:
public static boolean firstStringOfArrayIsTheSameAsTheWord(String word, String[] array) {
try {
return word.equals(array[0]);
} catch(/*NullPointerException or an ArrayIndexOutOfBoundsException*/ Exception e) {
return false;
}
}
Note that I do know that I could use multiple arguments in the if-statements, but I'm just interested in what method would be better-to-use in practice and why.
Thanks in advance!