I would wrap that whole thing up in a helper method, like this:
package com.test;
import java.text.ParseException;
public class Program {
private static int CheckIntegerArgument(String value, String name) {
try {
return Integer.parseInt(value);
}
catch(NumberFormatException e) {
throw new
IllegalArgumentException(String.format("%s is not a number (%s)", name, value));
}
}
public static void someMethod(String a, String b) {
int aValue = CheckIntegerArgument(a, "a");
int bValue = CheckIntegerArgument(b, "b");
}
public static void main(String[] args) throws Exception, ParseException {
try {
someMethod("1234", "I am not a number");
}
catch(IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
Hope this helps,
Fredrik Bornander