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