Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

which exception?

Thread view: 
josh - 14 Feb 2007 09:13 GMT
Hi,

which is the right exception to manage an "out of memory situation"
when I create an object with the new operator?

try
{
  Time t = new Time();
}catch(...)
{
 ////////  out of memory
}

Thanks
Michael Rauscher - 14 Feb 2007 09:29 GMT
josh schrieb:
> Hi,
>
> which is the right exception to manage an "out of memory situation"
> when I create an object with the new operator?

If the system runs out of memory, an OutOfMemoryError is thrown. An
Error (java.lang.Error) is not an Exception and therefore should not be
caught.

Bye
Michael
josh - 14 Feb 2007 09:40 GMT
> josh schrieb:
>
[quoted text clipped - 9 lines]
> Bye
> Michael

My problem is to convert C++ code like this:

Time *t;

try
{
  t = new Time(15,10,00);
}
catch(bad_alloc ba)
{
  cout << "OUT OF MEMORY!!\n";
  return EXIT_FAILURE;
}

so how I can do that?
Michael Rauscher - 14 Feb 2007 10:03 GMT
> My problem is to convert C++ code like this:
>
[quoted text clipped - 11 lines]
>
> so how I can do that?

It's easy: don't mind the "out of memory situation".

The Java equivalent would be:

Time t = new Time(15,10,00);

There is a distinction in Java between Errors and Exceptions (both are
subtypes of java.lang.Throwable). Errors indicates conditions where it
doesn't make (much) sense to handle them - more: the documentation says
that one should not handle them.

Bye
Michael
Chris Dollin - 14 Feb 2007 10:16 GMT
> My problem is to convert C++ code like this:
>
[quoted text clipped - 11 lines]
>
> so how I can do that?

   Time t = new Time( 15, 10, 00 );

If it runs out of memory, an error is thrown and the program will
terminate, just as the C++ does.

(Unless there's an outer shell that catches and handles it, of
course, which in principle there /could/ be. But then, you shouldn't
try and second-guess that.)

Signature

Chris "electric hedgehog" Dollin
"It's just the beginning we've seen" - Colosseum, /Tomorrow's Blues/

Chris Dollin - 14 Feb 2007 10:36 GMT
>> try
>> {
[quoted text clipped - 12 lines]
> If it runs out of memory, an error is thrown and the program will
> terminate, just as the C++ does.

Duh. Somehow I read an `exit(EXIT_FAILURE)` rather than a `return`
up there. Today, I'm an idiot. Ignore my previous post.

Signature

Chris "electric idiot" Dollin
There' no hortage of vowel on Uenet.

Daniel Pitts - 14 Feb 2007 19:37 GMT
> >> try
> >> {
[quoted text clipped - 19 lines]
> Chris "electric idiot" Dollin
> There' no hortage of vowel on Uenet.

In C++, I thought the "new" operator returned NULL if there wasn't
enough memory...

In any case, The concerns of Memory management are different in Java
than in C++.  Don't try to "handle" out of memory exceptions, as they
generally imply a bug, rather than a true resource limitation.
Chris Uppal - 14 Feb 2007 18:52 GMT
> My problem is to convert C++ code like this:
>
[quoted text clipped - 9 lines]
>    return EXIT_FAILURE;
> }

The API implied by the C++ code fragment is not suitable for use in Java.
Normally it is a bad idea to return error status for exeptional conditions,
exceptions are intended to be used in such circumstances.  So, a first
approximation to that code (in Java) would be

   try
   {
       t = new Time(15,10,00);
   }
   catch (/* what goes here ??*/)
   {
       system.err.println("Out of Memory !!");
       throw new MyOutOfMemoryException();
  }

But that has some problems -- it's not likely that you'd be able to write to
system.err if the program was out of memory, for one (though there /might/ be
more sophisticated ways of dealing with the problem which /did/ allow the
program to carry on).  So, we might have

   try
   {
       t = new Time(15,10,00);
   }
   catch (/* what goes here ??*/)
   {
       throw new MyOutOfMemoryException();
  }

But that, for most purposes is redundant, since the exeption that the system
throwns (OutOfMemoryError) is sufficient on its own.  So we can remove both the
catch and the throw:

       t = new Time(15,10,00);

Or -- if you've managed to find a way to allow your program to continue after
an OOME (not easy) -- then you might have

   try
   {
       t = new Time(15,10,00);
   }
   catch (OutOfMemoryError e)
   {
       invokeCleverRecoveryCode();
       throw new MyOperationFailedException(e);
  }

But that would be pretty unusual....

   -- chris
Tor Iver Wilhelmsen - 15 Feb 2007 18:15 GMT
> try
> {
[quoted text clipped - 5 lines]
>    return EXIT_FAILURE;
> }

Not necessarily "out of memory", but "out of free chunks of contigous
memory the size of the object". Java manages the memory - there is a
reason people write virtual memory/garbage collection systems for C++
programmers. So just don't worry about it.
Andrew Thompson - 14 Feb 2007 09:44 GMT
...
> which is the right exception to manage an "out of memory situation"
> when I create an object with the new operator?

'That does not happen.'

Or rather, it would be exceptional for
it to happen on the creation of 'a single
object' as suggested in the code snippet.

It might happen after creating 100's of
thousands, or millions of average objects,
but the JVM can work with astonishing
amounts of data before hitting memory
problems, even with the default memory
size.

Much more common in this situation, is
that the application has a memory leak.

I sughgest you prepare an SSCCE*, and
if that fails to reveal the problem,
post it here.

* <http://www.physci.org/codes/sscce>

In addition to what Michael was mentioning,
I will add that although Error's should
generally not be caught, an OOME *can*
be caught, and with a little preparation,
it is possible to show the end user an
'Error' dialog (at least) before the
program shuts down.

But look into the SSCCE first.

Andrew T.
Gordon Beaton - 14 Feb 2007 09:46 GMT
> which is the right exception to manage an "out of memory situation"
> when I create an object with the new operator?

If the constructor fails, an appropriate exception will be raised.

Why do you want to replace it with a different one, especially if you
don't know which what you want to replace it with?

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

josh - 14 Feb 2007 10:22 GMT
> If the constructor fails, an appropriate exception will be raised.

what? If I didn't want a generic Exception
Hendrik Maryns - 14 Feb 2007 15:23 GMT
josh schreef:
>> If the constructor fails, an appropriate exception will be raised.
>
> what? If I didn't want a generic Exception

It is not a generic Exception.  It is an ‘appropriate exception’.  In
this case, even an Error: OutOfMemoryError

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.