• 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

Am i entitled to throw an OutOfMemoryError ?

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

am currently writing a method that has the potential to
cause an OutOfMemoryError.

As things are, i can predict from the size of the input data (will
multiply by a certain factor), that inputData > 4096 bytes will lead
to an OutOfMemory Desaster.

I can throw an Exception, but which one?

Should i throw

- My own Exception aka ThisWillNotWorkException("too much inputdata")
- RuntimeException("too much inputdata")
- OutOfMemoryError("too much inputdata")

Any thoughts on this one ?

Thank you,

J.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another choice might be java.lang.IllegalArgumentException, or some other class from the standard API that expresses the idea you have in mind.

I think my first choice would be a custom class -- it's up to you whether to make it a RuntimeException subclass or not. Second choice would be IllegalArgumentException. Third choice would be something generic like RuntimeException, and I put throwing OutOfMemoryError last on the list, because the semantics are wrong; normally it's up to the catcher to do something about it, but here there would be no lasting problem.
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think i'll go for a custom Exception. Running the app, when this problem occurs is pretty pointless, so i'll catch it and die gracefully.

Thank you,

J.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand correctly, you basically say that the method shouldn't be called with inputData > 4096 bytes? Then I'd throw an IllegalArgumentException - and write my other code so that it doesn't call the method with such inputData. (You could also provide a method isInputDataValid, that returns a boolean instead of throwing an exception and can be called to check wether it would be legal to call the method using the data.)
 
reply
    Bookmark Topic Watch Topic
  • New Topic