This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Error message print Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error message print" Watch "Error message print" New topic

Error message print

nitinram agarwal
Ranch Hand

Joined: Jan 29, 2009
Posts: 83
I have a code which is throwing "java.lang.OutOfMemoryError:" sometime,
I want to print some specific value in my catch block before this error message.
please let me know how to do this.

for ex here is the code snippet

try {
} catch(Exception e) {
print_some_error_message; // I want to print this error message if outof memory is thrown
//usual exception is thrown here
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
nitinram agarwal wrote:hI,

Swastik Dey

Joined: Jan 08, 2009
Posts: 1474

Monu Tripathi

Joined: Oct 12, 2008
Posts: 1369

Throwable class has two subclasses: Error and Exception. Take a look at the inheritance tree of the OutOfMemoryError class. Also, check if you should catch such an error and if you do, what you can or cannot do.

[List of FAQs] | [Android FAQ] | [Samuh Varta]
Rob Spoor

Joined: Oct 27, 2005
Posts: 19684

Keep in mind that an OutOfMemoryError (OOME) indicates that the JVM doesn't have enough memory for some operation. This can very well mean that your code will cause another OOME, because it's quite likely there still isn't enough memory.

How To Ask Questions How To Answer Questions
David Newton

Joined: Sep 29, 2008
Posts: 12617

Swastik Dey wrote:

Why wouldn't you just catch the OutOfMemoryError?

In any case, you should consider whether or not this is something your application should even be catching: errors indicate a *serious* problem, one which is likely to prevent further execution.
It is sorta covered in the JavaRanch Style Guide.
subject: Error message print