示例

public static void main( String[] args ) {
int value = 0;
try {
value = Integer.parseInt( args[ 0 ] );
} catch ( NumberFormatException e0 ) {
try {
value = Integer.parseInt( args[ 1 ] );
} catch ( NumberFormatException e1 ) {
try {
value = Integer.parseInt( args[ 2 ] );
} catch ( NumberFormatException e2 ) {

}
}
}
}

解决方案
使用策略设计模式。
  1. 创建一个用于执行该任务的接口。
  2. 使执行该任务的方法抛出异常。
  3. 使具有嵌套的 try/catch 块的类成为策略的容器。
  4. 将嵌套的 try/catch 块更改为一个 for 循环
  5. 当循环中发生异常时,应用策略。

public static void main( String[] args ) {
int value = 0;
for ( int i = 0; i < args.length; i++ ) {
try {
value = Integer.parseInt( args[ i ] );
break ;
} catch ( NumberFormatException e ) {
// Ignore exception
}
}

}