try { ... int i = Integer.parseInt(str.substring(j,k)); ... } catch (StringIndexOutOfBoundsException e){ ... } catch (NumberFormatException e){ ... } catch (Exception e){ ... }