This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes parsing question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "parsing question" Watch "parsing question" New topic

parsing question

Faheem Khan

Joined: Feb 21, 2011
Posts: 3
Hi all,

I have a quick question about 'parse exceptions' in...

1. Integer.parseInt(...)
2. DataFormat's parse() method.

I've noticed that Integer.parseInt() throws an unchecked run-time exception (NumberFormatException which extends IllegalArgumentException) when it can't parse an integer e.g.
int a = Integer.parseInt("4a");

But on the other hand... if we have

String someStringToParse = ...;
DateFormat df = DateFormat.getDateInstance();
}catch(ParseException p){

We're forced to handle (or declare) a ParseException.

Why didn't the Java designers have Integer.ParseInt(...) throw a ParseException or make the DateFormat parse method throw a DateFormatException(doesn't exist) or atleast make the NumberFormatException a checked exception for some kind of consistency in the different parsing methods?

Thanks for your time,

Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4272

Likely because the methods were designed by different architects with different ideas.

In my opinion, the date parsing method should have thrown an unchecked exception as well.

Both classes should then provide methods that let you check whether a given string is valid for parsing. Sadly, this isn't the case.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 295

I agree, unchecked Exception results in more cleaner code than checked exception so in this case DateFormat.parse() should throw unchecked Exception.

SQL Query Interview Question java67 Javarevisited
Faheem Khan

Joined: Feb 21, 2011
Posts: 3
Thanks for the replies

I guess it's something to look out for on the exam.
I agree. Here's the link:
subject: parsing question
It's not a secret anymore!