範例

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 ) {
// 忽略異常狀況
}
}

}