Another exception question

  • Thread starter Michael Satterwhite
  • Start date
M

Michael Satterwhite

Assume that I throw an exception in routine A using a string that I built
when I recognized the error.

When routine B receives control on catch, the string from above has gone out
of scope and now has undefined contents. What is the most common method of
solving this problem? I'd rather not use a variable with global scope.
 
A

Alf P. Steinbach

* Michael Satterwhite said:
Assume that I throw an exception in routine A using a string that I built
when I recognized the error.

When routine B receives control on catch, the string from above has gone out
of scope and now has undefined contents. What is the most common method of
solving this problem? I'd rather not use a variable with global scope.

Whatever the perceived problem is (do post code that shows the problem),
solve it by using standard exceptions.

Simple.
 
J

John Harrison

Michael Satterwhite said:
Assume that I throw an exception in routine A using a string that I built
when I recognized the error.

When routine B receives control on catch, the string from above has gone out
of scope and now has undefined contents. What is the most common method of
solving this problem? I'd rather not use a variable with global scope.

If by string you mean std::string then there is no problem. If by string you
mean char array then there is a problem and the solution is to use a
std::string.

john
 
C

Claudio Jolowicz

Assume that I throw an exception in routine A using a string that I built
when I recognized the error.

When routine B receives control on catch, the string from above has gone out
of scope and now has undefined contents. What is the most common method of
solving this problem? I'd rather not use a variable with global scope.

#include <string>
#include <iostream>

int getMeaningOfLife()
{
throw std::string("Ran out of time...");
return 42;
}

int main() try
{
std::cout << getMeaningOfLife() << std::endl;
return 0;
}
catch (std::string s)
{
std::cout << s << std::endl;
return 1;
}


If you want to avoid copying, you can use a reference parameter:


catch (const std::string& s)
{
std::cout << s << std::endl;
return 1;
}

The temporary string will not be destroyed as long as the constant
reference is bound to it. But don't throw a local variable in this case!
 
S

Siemel Naran

Michael Satterwhite said:
Assume that I throw an exception in routine A using a string that I built
when I recognized the error.

If you built it using std::string or something, and the exception object
makes a copy of the std::string, then you're all safe.
When routine B receives control on catch, the string from above has gone out
of scope and now has undefined contents. What is the most common method of
solving this problem? I'd rather not use a variable with global scope.

Consider deriving your exception class from std::runtime_error or one of the
others in <stdexcept> as these contain a std::string in them.

There is the issue of the exception object's copy constructor throwing an
exception, say for out of memory, when copying the std::string. Then you
throw the out of memory exception or std::bad_alloc, then the one you
wanted, or maybe you have two exceptions in effect. The STLPort solves this
problem by storing in std::runtime_error not a std::string but instead a
char[256] so there's no dynamic memory to manage. Keep this in mind. Look
at the file <stdexcept> to see what it does.
 

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

Members online

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top