• Post Reply Bookmark Topic Watch Topic
  • New Topic

Am i entitled to throw an OutOfMemoryError ?  RSS feed

 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.)
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!