This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Overflow Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Java in General
Bookmark "Overflow Exception" Watch "Overflow Exception" New topic

Overflow Exception

Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 118
I had a job interview today where I was asked to write a Java method that took a String object as input and outputted the int it was equivalent to. I needed to throw an exception if the number was too big, and my interviewer indicated there is an existing exception I can use that indicates overflow. Anybody have any idea what that exception is?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14950

I would use an IllegalArgumentException. It doesn't specifically indicate overflow, but it means that an argument that was passed to a method was invalid for whatever reason.

Did you have a look in the API documentation for exceptions that could match what you're looking for?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3027
NumberFormatException is a subclass of IllegalArgumentException that could be used here. In fact, it [i]is[/] used by the Integer.parseInt() method, which seems to be equivalent to what the applicant is being asked to write.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43892
Java™ does not usually throw Exceptions for overflow errors. The error you are suffering is not overflow. Overflow is where you add 1 to 2147483647 and get …
I agree. Here's the link:
subject: Overflow Exception
jQuery in Action, 3rd edition