import java.util.regex.Pattern; import java.util.regex.Matcher; /** * @author Andreas Prudzilko */ public class Test { public static void main(String[] args) { String[] myarr = new String[]{ "a", "b", "c", "d", "e", "f", "g", "h", "j" }; int length = -1; try { String foo; for (int i = 0; i < Integer.MAX_VALUE ; foo = myarr[i++]); } catch (IndexOutOfBoundsException e) { Pattern p = Pattern.compile("\\d+"); String message = e.getMessage(); Matcher m = p.matcher(message); if (m.matches()) length = Integer.parseInt(m.group(0)); } System.out.println("length: "+length); } }