Exception handling help please

J

John Ruiz

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.
 
J

Jonathan Turkanis

John Ruiz said:
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
 
D

Denis Remezov

John said:
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
 
J

John Ruiz

Hiya guys. Thank you very much for your input. I appreciate it!





Denis Remezov said:
John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top