Hello. I was wondering about exceptions and how to throw them within
functions/methods and catch them. Suppose that we've got:
----------------------------------------------------------------
void MyFunc()
{
//...
if (somethingBad)
throw MyException("Throwing exception");
//...
}
Then in the caller:
try
{
MyFunc()
}
catch (MyException e)
{
//...
}
...
----------------------------------------------------------------
The problem I have is that MyFunc() returns a local copy of MyException
correct? So isn't that inappropriate then? Or when the caller reaches the
catch block, the variable then goes out of scope so that makes it alright?
What if though the caller doesn't even catch the exception? Where does
MyException from MyFunc() go then? I'm confused, so I'm thinking something
like this then:
----------------------------------------------------------------
void MyFunc()
{
//...
if (somethingBad)
throw new MyException("Throwing exception");
//...
}
The caller
try
{
MyFunc()
}
catch (MyException *e)
{
//...
delete e;
}
...
----------------------------------------------------------------
To me, that seems right. However, isn't there a potential for a memory
leak:
----------------------------------------------------------------
void MyFunc()
{
...
if (somethingBad)
throw new MyException("Throwing exception");
...
}
try
{
MyFunc()
}
catch (...)
{
//...
}
//...
----------------------------------------------------------------
Directly above, the caller doesn't catch *MyException, and if the caller
still does stuff after the catch block, then isn't there a memory leak after
that? Also, if the caller doesn't even bother to catch anything and just
calls MyFunc(), then isn't that a memory leak also if MyFunc() returns
*MyException? So, I'm thinking then that we should never return a pointer
because only because C++ doesn't force exception handling, so there's a
potential for a memory leak correct? If exceptions were forced to be
caught, then at least the caller could delete the pointer. Please someone
make this clear for me, and please understand that I am a beginner at
exception handling in C++. Thanks in advance. 3 1332
"John Ruiz" <to*********@hotmail.com> wrote in message
news:7R*****************@fe39.usenetserver.com... Hello. I was wondering about exceptions and how to throw them
within functions/methods and catch them. Suppose that we've got:
---------------------------------------------------------------- void MyFunc() { //... if (somethingBad) throw MyException("Throwing exception"); //... }
Then in the caller:
try { MyFunc() } catch (MyException e) { //... } ... ----------------------------------------------------------------
The problem I have is that MyFunc() returns a local copy of
MyException correct? So isn't that inappropriate then? Or when the caller
reaches the catch block, the variable then goes out of scope so that makes it
alright? What if though the caller doesn't even catch the exception? Where
does MyException from MyFunc() go then?
When
throw MyException("Throwing exception");
is executed, a copy of the local MyException object is made and this
copy is used to initialize exception variables declared in catch ( ).
So everything works fine.
By the way, its a good idea to catch exceptions by reference or const
reference.
Jonathan
John Ruiz wrote: Hello. I was wondering about exceptions and how to throw them within functions/methods and catch them. Suppose that we've got:
---------------------------------------------------------------- void MyFunc() { //... if (somethingBad) throw MyException("Throwing exception"); //... }
Then in the caller:
try { MyFunc() } catch (MyException e) { //... } ... ----------------------------------------------------------------
[...]
As Jonathan noted, the above is fine, but it's better to catch by
reference:
catch(MyException const& e);
or, if necessary,
catch(MyException& e);
First, it will likely eliminate one unnecessary copy of e.
Second, if you throw an object of a class derived from MyException,
e will actually refer to an object of the derived class. If you
catch by value, e will have the dynamic type of MyException, will
be copy-constructed from the original object (or a copy thereof),
and so will lose the derived class information.
The copy of the exception object specified in the throw expression
can and likely will be eliminated (add instrumentation to
MyException and see for yourself; it helps to /define/ the
exception object in the throw expression, like in your example).
It is quite possible that you will see only one instance of
MyException per throw/catch. You don't have to worry where the
storage for this object comes from (it's unspecified). ---------------------------------------------------------------- void MyFunc() { //... if (somethingBad) throw new MyException("Throwing exception"); //... }
The caller
try { MyFunc() } catch (MyException *e) { //... delete e; } ... ----------------------------------------------------------------
To me, that seems right. However, isn't there a potential for a memory leak:
---------------------------------------------------------------- void MyFunc() { ... if (somethingBad) throw new MyException("Throwing exception"); ... }
try { MyFunc() } catch (...) { //... } //... ---------------------------------------------------------------- Directly above, the caller doesn't catch *MyException, and if the caller still does stuff after the catch block, then isn't there a memory leak after that? Also, if the caller doesn't even bother to catch anything and just calls MyFunc(), then isn't that a memory leak also if MyFunc() returns *MyException? So, I'm thinking then that we should never return a pointer because only because C++ doesn't force exception handling, so there's a potential for a memory leak correct? If exceptions were forced to be caught, then at least the caller could delete the pointer.
To further complicate things, it is not immediately obvious whether you
should call delete on the pointer when you catch the exception. It could
be pointing to an object created with new or to a static object.
Generally, you cannot be sure without scanning through all the code
that could have thrown the exception.
Denis
Hiya guys. Thank you very much for your input. I appreciate it!
"Denis Remezov" <fi***************@yahoo.removethis.ca> wrote in message
news:40***************@yahoo.removethis.ca... John Ruiz wrote: Hello. I was wondering about exceptions and how to throw them within functions/methods and catch them. Suppose that we've got:
---------------------------------------------------------------- void MyFunc() { //... if (somethingBad) throw MyException("Throwing exception"); //... }
Then in the caller:
try { MyFunc() } catch (MyException e) { //... } ... ----------------------------------------------------------------
[...]
As Jonathan noted, the above is fine, but it's better to catch by reference: catch(MyException const& e); or, if necessary, catch(MyException& e);
First, it will likely eliminate one unnecessary copy of e. Second, if you throw an object of a class derived from MyException, e will actually refer to an object of the derived class. If you catch by value, e will have the dynamic type of MyException, will be copy-constructed from the original object (or a copy thereof), and so will lose the derived class information.
The copy of the exception object specified in the throw expression can and likely will be eliminated (add instrumentation to MyException and see for yourself; it helps to /define/ the exception object in the throw expression, like in your example). It is quite possible that you will see only one instance of MyException per throw/catch. You don't have to worry where the storage for this object comes from (it's unspecified).
---------------------------------------------------------------- void MyFunc() { //... if (somethingBad) throw new MyException("Throwing exception"); //... }
The caller
try { MyFunc() } catch (MyException *e) { //... delete e; } ... ----------------------------------------------------------------
To me, that seems right. However, isn't there a potential for a memory leak:
---------------------------------------------------------------- void MyFunc() { ... if (somethingBad) throw new MyException("Throwing exception"); ... }
try { MyFunc() } catch (...) { //... } //... ---------------------------------------------------------------- Directly above, the caller doesn't catch *MyException, and if the caller still does stuff after the catch block, then isn't there a memory leak
after that? Also, if the caller doesn't even bother to catch anything and
just calls MyFunc(), then isn't that a memory leak also if MyFunc() returns *MyException? So, I'm thinking then that we should never return a
pointer because only because C++ doesn't force exception handling, so there's a potential for a memory leak correct? If exceptions were forced to be caught, then at least the caller could delete the pointer.
To further complicate things, it is not immediately obvious whether you should call delete on the pointer when you catch the exception. It could be pointing to an object created with new or to a static object. Generally, you cannot be sure without scanning through all the code that could have thrown the exception.
Denis This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Daniel Wilson |
last post by:
I am having exception-handling and stability problems with .NET. I will
have a block of managed code inside try...catch and will still get a generic
..NET exception box that will tell me which...
|
by: Noor |
last post by:
please tell the technique of centralize exception handling without try catch
blocks in c#.
|
by: Master of C++ |
last post by:
Hi,
I am an absolute newbie to Exception Handling, and I am trying to
retrofit exception handling to a LOT of C++ code that I've written
earlier. I am just looking for a bare-bones, low-tech...
|
by: Zorro |
last post by:
The simplicity of stack unraveling of C++ is not without defective
consequences. The following article points to C++ examples showing the
defects. An engineer aware of defects can avoid...
|
by: jayapal |
last post by:
Hi ,
I am using the NEW operator to allocate the memory in many places of
my code.But I am not doing any error hadling or exception handling.Can
any one suggests me how to do exception handling,...
| |
by: George2 |
last post by:
Hello everyone,
Such code segment is used to check whether function call or exception-
handling mechanism runs out of memory first (written by Bjarne),
void perverted()
{
try{
|
by: =?Utf-8?B?UG9sbHkgQW5uYQ==?= |
last post by:
Hi,
I have previously used EL v 3.1 Exception Handling application block
successfully. I thought I would now try to do the same with EL v 4.0. My
first experiment was to replace an exception....
|
by: srizzler |
last post by:
Hi All:
I am trying to implement Exception Handling using Enterprise Library 3.1's Exception Handling Application Block as well as Logging Blocks.
I have a windows application developed in...
|
by: Steve |
last post by:
Hi All
I have a windows forms Application (SAM) in vb.net 2008 using .net framework
V2
One and only one customer out of 30 customers is getting errors daily where
they have to close and...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |