Topic: Q: standard exceptions
Author: Oleg Krivosheev <kriol@fnal.gov>
Date: 1997/06/28 Raw View
Hi, All
can someone provide clarifications why
standard exceptions (see <stdexcept> in CD2)
accept string as constructor argument but
return const char* after what() memeber call.
class logic_error : public exception {
public:
logic_error(const string& what_arg); // string as argument
};
class exception {
public:
exception() throw();
exception(const exception&) throw();
exception& operator=(const exception&) throw();
virtual ~exception() throw();
virtual const char* what() const throw(); // const char* as result !!!
};
sincerely
OK
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Steve Clamage <stephen.clamage@Eng.Sun.COM>
Date: 1997/07/01 Raw View
Oleg Krivosheev wrote:
>
> can someone provide clarifications why
> standard exceptions (see <stdexcept> in CD2)
> accept string as constructor argument but
> return const char* after what() memeber call.
That topic was the subject of considerable debate in
the C++ Committee.
Using strings everywhere seems like The Right Thing
To Do. Strings are safer than char* and char[]. But it
also means that any program that might involve an
exception is going to have to pull in the string library.
That wouldn't be so bad, except that C++ is also supposed
to support "stand-alone" implementations, primarily for
use with embedded systems. A stand-alone system need not
contain any of the major library components.
Some exceptions are wired into the language, independent
of the use of any of the standard library. One example
is dynamic_cast with reference types. If exceptions always
involved strings, a user program would not be able to
avoid using the string library.
Consequently, the exception base class does not use
strings at all. Embedded systems and their programs
can thus avoid using strings if they wish. Programmers
who don't mind using strings can get more detailed
information about exceptions.
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]