Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overflow Exception

 
Kevin Simonson
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 …
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic