Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > which exception?

Reply
Thread Tools

which exception?

 
 
Chris Uppal
Guest
Posts: n/a
 
      02-14-2007
josh wrote:

> 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


 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      02-14-2007
On Feb 14, 2:36 am, Chris Dollin <(E-Mail Removed)> wrote:
> Chris Dollin wrote:
> >> 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.

>
> Duh. Somehow I read an `exit(EXIT_FAILURE)` rather than a `return`
> up there. Today, I'm an idiot. Ignore my previous post.
>
> --
> 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.

 
Reply With Quote
 
 
 
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      02-15-2007
"josh" <(E-Mail Removed)> writes:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
Web Stats? Which to use? Which is best? Familyman HTML 3 02-09-2006 11:05 PM
ADSL WIC support - which NM's, and which IOS versions? Kralizec Craig Cisco 5 12-08-2005 02:20 AM
which XMI version compatible to which UML version? Kenny XML 0 06-02-2004 10:20 PM
Keeping track of which user controls need to be loaded and which not John ASP .Net 0 07-08-2003 09:26 AM



Advertisments