• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overflow Exception

 
Ranch Hand
Posts: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 …
 
reply
    Bookmark Topic Watch Topic
  • New Topic