Re: Why does std::exception constructor specifies it can throw any ?
From: Siemel Naran (SiemelNaran_at_REMOVE.att.net)
Date: 03/03/05
- Next message: David: "Re: client -server interaction over XML supporting multiple protocols"
- Previous message: Jonathan Turkanis: "Re: Initializing a subset of state"
- In reply to: Pierre Rouleau: "Why does std::exception constructor specifies it can throw any ?"
- Next in thread: Pierre Rouleau: "Re: Why does std::exception constructor specifies it can throw any ?"
- Reply: Pierre Rouleau: "Re: Why does std::exception constructor specifies it can throw any ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 03 Mar 2005 06:52:19 GMT
"Pierre Rouleau" <Pierre_Rouleau@impathnetworks.com> wrote in message
news:J6nVd.29482
> The std::exception class defined in the Standard C++ <exception> header
> specifies that the constructors could throw any exception becuase they
> do not have a throw() specification.
>
> Why is that? Is this because there could be an exception thrown when
> the code creates a std::exception? I would assume that is not the case.
All of the 5 functions of class std::exception are declared with throw(),
according to the standard. The constructor, copy constructor, operator=,
and destructor do nothing (though the standard does not require that they do
nothing), so of course they don't throw. The pure virtual what function
must be defined in the derived classes as not throwing exceptions either (so
it will have to return a string stored inside the exception object).
But the constructor of the derived class may throw. If your exception
stores the names and values of the environment variables, this means
(usually) allocating dynamic memory to store all the info, and thus the
constructor may throw if it runs out of memory.
The throw() specification on a function does not just mean that the function
does not throw, but rather that the function throws nothing or calls
std::terminate. The call to std::terminate happens if the function throws
an exception either directly or calls a function (virtual or not) that
throws an exception.
If you declare your derived class as
class DerivedException : public std::exception {
public:
DerivedException() throw() { }
};
does it fail compile? It passes compile on my computer, Borland C++ 6.
> However, if I want to create a new exception class, derived from
> std::exception (say MyException) then how can I guarantee that creating
> an instance of MyException will not generate any exception?
You can use try-catch.
DerivedException::DerivedException() {
try { stuff that may throw; }
catch (...) { }
}
But be aware that the compiler generated initialization list calls the
default constructor of the base class, plus the default constructor of
contained objects. If any of these throw, then you program will call
std::terminate. So be sure that the base class constructor does not throw,
which is the case for std::exception, and that the constructors of your
contained objects don't throw.
To garauntee this, you could allocate objects on the heap, for example:
class MyException : public std::exception {
public:
MyException() throw();
const char * what() const throw() { return d_what; }
private:
struct Details;
Details * d_details;
const char *const d_what;
};
struct MyException::Details {
std::string name;
std::string value;
}
MyException::MyException() : std::exception(), d_details(), d_what() {
try {
d_details = new Details;
d_details->name = "hello";
d_details->value = "world";
}
catch (...) { delete d_details; d_what = NULL; }
}
> class MyException : public std::exception
> {
> public:
> explicit MyException(int someinfo) throw();
> // violates specs of std::exception
> ....
> };
>
> Thanks for any information on this topic.
- Next message: David: "Re: client -server interaction over XML supporting multiple protocols"
- Previous message: Jonathan Turkanis: "Re: Initializing a subset of state"
- In reply to: Pierre Rouleau: "Why does std::exception constructor specifies it can throw any ?"
- Next in thread: Pierre Rouleau: "Re: Why does std::exception constructor specifies it can throw any ?"
- Reply: Pierre Rouleau: "Re: Why does std::exception constructor specifies it can throw any ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|