I've been investigating Java's ParseException, when trying to write my own method that parses from a String to an int, and have two questions:
- When should I throw it? Java's Integer.parseInt(String string) throws a NumberFormatException when an invalid input is given (makes sense with it being a subclass of IllegalArgumentException), and does not throw ParseException at all.
- What is "errorOffset"? ParseException's constructor forces you to give it a value - what does it mean?
Throwing a ParseException for a parseXYZ() method (that actually parses something) is perfectly valid. Integer.parseInt throws a NumberFormatException since it is a subtype of RuntimeException and therefore does not force you to surround it with try-catch (This is not true for ParseException).
The offset field is used to identify the position for which the error occurred. With that information, you may be able to use whatever partial data you already parsed.