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