Is catching NumberFormatException a bad practice?
Posted
by
integeruser
on Stack Overflow
See other posts from Stack Overflow
or by integeruser
Published on 2012-06-22T19:12:36Z
Indexed on
2012/06/27
21:16 UTC
Read the original article
Hit count: 173
I have to parse a String that can assume hex values or other non-hex values
0xff
, 0x31
or A
, PC
, label
, and so on.
I use this code to divide the two cases:
String input = readInput();
try {
int hex = Integer.decode(input);
// use hex ...
} catch (NumberFormatException e) {
// input is not a hex, continue parsing
}
Can this code be considered "ugly" or difficult to read? Are there other (maybe more elegant) solutions?
EDIT : I want to clarify that (in my case) a wrong input doesn't exist: i just need to distinguish if it is a hex number, or not. And just for completeness, i'm making a simple assebler for DCPU-16.
© Stack Overflow or respective owner