Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Proper Way to catch exception thrown by new keyword

Reply
Thread Tools

Proper Way to catch exception thrown by new keyword

 
 
Fao, Sean
Guest
Posts: n/a
 
      07-05-2003
I'm reading The C++ Programming Language by Bjarne Stroustrup and I
was unclear as to the proper way to catch an exception thrown by the
new keyword. I was wondering if someobody here could let me know if I
have written the following code correctly.

<CODE_SNIP>
#include <iostream>
#include <new>

using std::cerr;
using std::bad_alloc;

int main()
{
char *p;

try
{
for(;
p = new char[100000];
}

catch(bad_alloc)
{
cerr << "Error allocating memory...\n";
return 1;
}

delete p;

return 0;
}

</CODE_SNIP>

I thank you in advance for any pointers you can provide.

Sean
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-05-2003

"Fao, Sean" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I'm reading The C++ Programming Language by Bjarne Stroustrup and I
> was unclear as to the proper way to catch an exception thrown by the
> new keyword. I was wondering if someobody here could let me know if I
> have written the following code correctly.


Well that depends on what you want it to do.

>
> <CODE_SNIP>
> #include <iostream>
> #include <new>
>
> using std::cerr;
> using std::bad_alloc;
>
> int main()
> {
> char *p;
>
> try
> {
> for(;
> p = new char[100000];


Are you trying to loop until you run out of memory?

If two allocations succeed then this loop will leak memory. Because the
second allocation will overwrite the first and you will never be able to
free that memory.

> }
>
> catch(bad_alloc)


This isn't wrong but usually you catch exceptions using a const reference,
to avoid copying the exception object unnecessarily.

catch (const bad_alloc&)

> {
> cerr << "Error allocating memory...\n";
> return 1;
> }
>
> delete p;
>
> return 0;
> }
>
> </CODE_SNIP>
>
> I thank you in advance for any pointers you can provide.
>
> Sean


Looks OK.

john


 
Reply With Quote
 
 
 
 
Unforgiven
Guest
Posts: n/a
 
      07-05-2003
Fao, Sean wrote:
> delete p;


In addition to what join said, this line will always be reached, even when
an exception occurs. You may want to add a check (initialise p to NULL and
check for that for instance, or place the delete still inside the try block)
whether you really have any memory to delete.

Also you are allocating with new[], do you should free with delete[]. It
doesn't make any difference here because char is a basic type, but it's good
practice to do it anyway: it adds clarity and gets you in the habit so you
don't forget it when it does make a difference.

--
Unforgiven

"Earth. It exists only in a corner of my memory."
Lord Dornkirk
The Vision of Escaflowne

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-05-2003

"Unforgiven" <(E-Mail Removed)> wrote in message
news:be6v2j$1pggb$(E-Mail Removed)...
> Fao, Sean wrote:
> > delete p;

>
> In addition to what join said, this line will always be reached, even when
> an exception occurs. You may want to add a check (initialise p to NULL and
> check for that for instance, or place the delete still inside the try

block)
> whether you really have any memory to delete.


Actually there's a return in the catch block, so the delete is not reached
when an exception occurs.

>
> Also you are allocating with new[], do you should free with delete[]. It
> doesn't make any difference here because char is a basic type, but it's

good
> practice to do it anyway: it adds clarity and gets you in the habit so you
> don't forget it when it does make a difference.


Good point.

john


 
Reply With Quote
 
Unforgiven
Guest
Posts: n/a
 
      07-05-2003
John Harrison wrote:
> "Unforgiven" <(E-Mail Removed)> wrote in message
> news:be6v2j$1pggb$(E-Mail Removed)...
>> Fao, Sean wrote:
>>> delete p;

>>
>> In addition to what join said, this line will always be reached,
>> even when an exception occurs. You may want to add a check
>> (initialise p to NULL and check for that for instance, or place the
>> delete still inside the try block) whether you really have any
>> memory to delete.

>
> Actually there's a return in the catch block, so the delete is not
> reached when an exception occurs.


Oops, didn't see that ^_^

--
Unforgiven

"Earth. It exists only in a corner of my memory."
Lord Dornkirk
The Vision of Escaflowne
 
Reply With Quote
 
Fao, Sean
Guest
Posts: n/a
 
      07-06-2003
"Fao, Sean" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I'm reading The C++ Programming Language by Bjarne Stroustrup and I
> was unclear as to the proper way to catch an exception thrown by the
> new keyword. I was wondering if someobody here could let me know if I
> have written the following code correctly.


I appreciate your comments, I have rewritten the code using your
recomendations.

Thank you much...

Sean

 
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
catch doesn't catch a thrown exception Marteno Rodia Java 5 08-05-2009 03:30 AM
Proper Destruction of Class Members when an Exception is Thrown inDestructor AnonMail2005@gmail.com C++ 8 03-02-2009 01:07 AM
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
Catch exception thrown by DropDownList set Declaratively drop ASP .Net 0 02-07-2007 08:31 PM
How to force client to catch exception thrown from failed constructor yancheng.cheok@gmail.com C++ 1 05-15-2006 06:21 AM



Advertisments