Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to Int in java - Likely bad data, need to avoid exceptions

Seeing as Java doesn't have nullable types, nor does it have a TryParse(), how do you handle input validation without throwing an exceptions?

The usual way:

String userdata = /*value from gui*/ int val; try {    val = Integer.parseInt(userdata); } catch (NumberFormatException nfe) {    // bad data - set to sentinel    val = Integer.MIN_VALUE; } 

I could use a regex to check if it's parseable, but that seems like a lot of overhead as well.

What's the best practice for handling this situation?

EDIT: Rationale: There's been a lot of talk on SO about exception handling, and the general attitude is that exceptions should be used for unexpected scenarios only. However, I think bad user input is EXPECTED, not rare. Yes, it really is an academic point.

Further Edits:

Some of the answers demonstrate exactly what is wrong with SO. You ignore the question being asked, and answer another question that has nothing to do with it. The question isn't asking about transition between layers. The question isn't asking what to return if the number is un-parseable. For all you know, val = Integer.MIN_VALUE; is exactly the right option for the application that this completely context free code snippet was take from.

like image 842
Chris Cudmore Avatar asked Oct 06 '08 14:10

Chris Cudmore


People also ask

How do you avoid throwing exceptions in Java?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

Which exceptions can not be handled in Java?

Unchecked exceptions are error conditions that cannot be anticipated and recovered from. They are usually programming errors and cannot be handled at runtime. Unchecked exceptions are subclasses of java.

Should I use exceptions in Java?

Exceptions in Java are an important instrument to signal abnormal (or exceptional) conditions in the program flow which may prevent it to make a further progress.

Why catching all exceptions is bad?

Because when you catch exception you're supposed to handle it properly. And you cannot expect to handle all kind of exceptions in your code. Also when you catch all exceptions, you may get an exception that cannot deal with and prevent code that is upper in the stack to handle it properly.


1 Answers

I asked if there were open source utility libraries that had methods to do this parsing for you and the answer is yes!

From Apache Commons Lang you can use NumberUtils.toInt:

// returns defaultValue if the string cannot be parsed. int i = org.apache.commons.lang.math.NumberUtils.toInt(s, defaultValue); 

From Google Guava you can use Ints.tryParse:

// returns null if the string cannot be parsed // Will throw a NullPointerException if the string is null Integer i = com.google.common.primitives.Ints.tryParse(s); 

There is no need to write your own methods to parse numbers without throwing exceptions.

like image 137
Stephen Ostermiller Avatar answered Sep 19 '22 22:09

Stephen Ostermiller