This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes How to trap an OutOfMemoryError in making Xml Dom Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to trap an OutOfMemoryError in making Xml Dom" Watch "How to trap an OutOfMemoryError in making Xml Dom" New topic
Author

How to trap an OutOfMemoryError in making Xml Dom

Jain Saurabh
Greenhorn

Joined: Sep 05, 2001
Posts: 26
Hello !
Can somebody help me to trap the OutOfMemoryError in case of building the Xml Dom. If i go on appending the child to the parent element indefinitely then java throws OutOfMemoryError which i have placed in the try-catch statement but whenever this error is thrown my code in catch statement is not executed.I am using Apache Xerces Parser with java for making Xml Dom.But in normal circumstances if i go on appending a string infinitely so that the jvm goes OutOfMemoryError i am able to successfully trap it in my catch statement.why is this so???
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
OutOfMemoryError is a runtime error. Unlike checked exceptions that can be caught using the try-catch block, you can't trap RuntimeExceptions and subclasses of Error.
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Jain Saurabh
Greenhorn

Joined: Sep 05, 2001
Posts: 26
Hi Ajith !
I think you are mistaken try executing this code, it will display an optionpane when it will go out of memory.
static String str="a";
public static void main(String args[])
{

try
{
for(;
str+=str;
}
catch(java.lang.OutOfMemoryError e)
{ javax.swing.JOptionPane.showMessageDialognull,"OutOfMEmory caught");
}
}
but this doesn't happen when i go on appending child in Xmldom using Apache xerces in java.
hirdesh
Greenhorn

Joined: Sep 11, 2001
Posts: 9
kindly switch off the smileys before posing code as the code portion turn into smileys. I hope was able to convey my message.
------------------
Thanks,
Hirdesh
Jain Saurabh
Greenhorn

Joined: Sep 05, 2001
Posts: 26
Hi Ajith !
I think you are mistaken try executing this code, it will display an optionpane when it will go out of memory.
static String str="a";
public static void main(String args[])
{
try
{
for(;;)
str+=str;
}
catch(java.lang.OutOfMemoryError e)
{ javax.swing.JOptionPane.showMessageDialognull,"OutOfMEmory caught");
}
}
but this doesn't happen when i go on appending child in Xmldom using Apache xerces in java.
Hope i have now articulated myself more clearly.
Hoping a technical response this time.

Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
I should have said " you [/i]shouldn't[/i] trap ...." instead of " you can't trap ". The Java API documentation for
java.lang.Error, the superclass of java.lang.OutofMemoryError says -

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.
A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.

Hope that helps!
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to trap an OutOfMemoryError in making Xml Dom