aspose file tools*
The moose likes Java in General and the fly likes Am i entitled to throw an OutOfMemoryError ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Am i entitled to throw an OutOfMemoryError ?" Watch "Am i entitled to throw an OutOfMemoryError ?" New topic
Author

Am i entitled to throw an OutOfMemoryError ?

Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149
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
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149
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

Joined: Jul 11, 2001
Posts: 14112
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.)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Am i entitled to throw an OutOfMemoryError ?
 
Similar Threads
read a file ?
OutOfMemoryError when using ResultSet
Exception Connection reset
Can't declare methods to throw java.rmi.RemoteException
Trials and tribulations, trys and catches