Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the proper way to handle a NumberFormatException when it is expected?
    primarykey
    data
    text
    <p>I'm running into this situation where I need to parse a <code>String</code> into an <code>int</code> and I don't know what to do with the <code>NumberFormatException</code>. The compiler doesn't complain when I don't catch it, but I just want to make sure that I'm handling this situation properly.</p> <pre><code>private int getCurrentPieceAsInt() { int i = 0; try { i = Integer.parseInt(this.getCurrentPiece()); } catch (NumberFormatException e) { i = 0; } return i; } </code></pre> <p>I want to just simplify my code like this. The compiler doesn't have a problem with it, but the thread dies on the <code>NumberFormatException</code>.</p> <pre><code>private int getCurrentPieceAsInt() { int i = 0; i = Integer.parseInt(this.getCurrentPiece()); return i; } </code></pre> <p>Google CodePro wants me to log the exception in some way, and I agree that this is best practice.</p> <pre><code>private int getCurrentPieceAsInt() { int i = 0; try { i = Integer.parseInt(this.getCurrentPiece()); } catch (NumberFormatException e) { i = 0; e.printStackTrace(); } return i; } </code></pre> <p>I want this method to return <code>0</code> when the current piece is not a number or cannot be parsed. When I don't catch the <code>NumberFormatException</code> explicitly, does it not assign the variable <code>i</code>? Or is there some default value that <code>Integer.parseInt()</code> returns?</p> <p>General style says that if I catch an exception, I should log it somewhere. I don't want to log it. It's normal operation for this exception to be thrown sometimes, which also doesn't sit well with me. I cannot find a function, however, which will tell me if <code>Integer.parseInt()</code> will throw an exception. So my only course of action seems to be to just call it and catch the exception.</p> <p>The <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String)" rel="noreferrer">javadoc</a> for <code>parseInt</code> doesn't help much.</p> <p>Here are the specific questions I'd like to know:</p> <ul> <li>Is there a method that I can call that will tell me if <code>Integer.parseInt()</code> will throw a <code>NumberFormatException</code> before calling it? Then I would have no problem logging this, since it should never happen.</li> <li>If I simply do not catch the exception, will the valiable not get assigned? Then I will simply initialize it to the value that I want when it's not a number and not catch the exception.</li> <li>Is there a way to mark the exception somehow explicitly that I don't care about it? I'm thinking this would be something similar to <code>AWTEvent.consume()</code>. If so, then I will do this so that Google CodePro doesn't see this as "unlogged".</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload