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("Th rowing 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("Th rowing 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("Th rowing 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 1357
"John Ruiz" <to*********@ho tmail.com> wrote in message
news:7R******** *********@fe39. usenetserver.co m... 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("Th rowing 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("Th rowing 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("Th rowing 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(MyExcepti on const& e);
or, if necessary,
catch(MyExcepti on& 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("Th rowing 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("Th rowing 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.remov ethis.ca> wrote in message
news:40******** *******@yahoo.r emovethis.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("Th rowing 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(MyExcepti on const& e); or, if necessary, catch(MyExcepti on& 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("Th rowing 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("Th rowing 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 assemblies are loaded before
shutting down. In one case, some of my DB-accessing code didn't handle a
NULL value properly. But try...catch wouldn't catch the exception and keep
going. I'd just get the error message and then it would shut the...
|
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 exception
handling mechanism which will allow me to pass character information
about an error and its location from lower-level classes.
Can you please critique the following exception handling mechanism in
terms of my requirements ?
|
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 hard-to-find bugs.
http://distributed-software.blogspot.com/2007/01/c-exception-handling-is-defective.html
Regards,
zorabi@ZHMicro.com
http://www.zhmicro.com
http://distributed-software.blogspot.com
|
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, which code part I
have to add to do the exception handling
Thanks in advance,
..
| |
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.
I created a project and added the following references -
1/ Enterprise Library Exception Handling Application Block v 4.0
|
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 VB.Net (Visual Studio 2005) which have three tiers:
User Interface
Business Logic
Data Layer
|
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 restart my application, sometimes several times a day
The customer has XP Home SP2
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |