6
有效的try / catch块用法?
应该使用catch块来编写逻辑,即处理流控制等吗?还是仅仅为了抛出异常?它会影响代码的效率或可维护性吗? 在catch块中写逻辑有什么副作用(如果有)? 编辑: 我见过一个Java SDK类,其中他们在catch块内编写了逻辑。例如(摘录自java.lang.Integer课堂): try { result = Integer.valueOf(nm.substring(index), radix); result = negative ? new Integer(-result.intValue()) : result; } catch (NumberFormatException e) { String constant = negative ? new String("-" + nm.substring(index)) : nm.substring(index); result = Integer.valueOf(constant, radix); } 编辑2: 我正在阅读一个教程,他们将其视为在异常内编写例外情况逻辑的优势: 异常使您能够编写代码的主要流程,并处理其他地方的特殊情况。 有什么具体准则,何时在catch块中编写逻辑,何时不编写逻辑?