which exception?

J

josh

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
 
M

Michael Rauscher

josh said:
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
 
J

josh

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

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?
 
A

Andrew Thompson

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.
 
G

Gordon Beaton

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
 
M

Michael Rauscher

josh said:
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?

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
 
C

Chris Dollin

josh said:
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?

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.)
 
C

Chris Dollin

Chris said:
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.

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

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

josh schreef:
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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFF0ymNe+7xMGD3itQRAnvgAJ0cN0LqtolbFVUBSfS+07wfd9Na5wCeJQ+b
qrQrvg9lxhCfrwpK4hi47Bw=
=U4rS
-----END PGP SIGNATURE-----
 
C

Chris Uppal

josh said:
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;
}

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
 
D

Daniel Pitts

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


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.
 
T

Tor Iver Wilhelmsen

josh said:
try
{
t = new Time(15,10,00);
}
catch(bad_alloc ba)
{
cout << "OUT OF MEMORY!!\n";
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top