範例

public static void main( String[] args ) {

for ( int i = 0; i < args.length; i++ ) {
try {
System.out.println( Integer.parseInt( args[ i ] ) );
} catch ( NumberFormatException e ) {
throw new IllegalArgumentException( e.getMessage() );
}
}
}

解決方案
將 try/catch 區塊移至迴圈之外。

public static void main( String[] args ) {

try {
for ( int i = 0; i < args.length; i++ ) {
System.out.println( Integer.parseInt( args[ i ] ) );
}
} catch ( NumberFormatException e ) {
throw new IllegalArgumentException( e.getMessage() );
}

}