In this phrase, we use the parseInt() static method of the Integer class to attempt to convert the string parameter into an integer. If the string parameter cannot be converted into a valid integer, the NumberFormatException is thrown. Therefore, if we do not get a NumberFormatException, we can safely assume that the parseInt() method was able to parse the string into an integer value. You may want to consider declaring the int variable outside of the try block so that if the NumberFormatException is thrown, you can assign a default value to the variable in the catch block. The code for this technique would be as follows: int result = 0; try { result = Integer.parseInt(aString); } catch (NumberFormatException ex) { result = DEFAULT_VALUE; } |