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