Why use an exception instead of if…else
问题 For example, in the case of "The array index out of bound" exception, why don't we check the array length in advance: if(array.length < countNum) { //logic } else { //replace using exception } My question is, why choose to use an exception? and when to use an exception, instead of if-else Thanks. 回答1: They are used to inform the code that calls your code an exceptional condition occurred. Exceptions are more expensive than well formed if/else logic so you use them in exceptional circumstances