Re: Throwing a simple exception

From: Computer Whizz (old486whizz_at_hotmail.com)
Date: 10/24/04


Date: Sun, 24 Oct 2004 11:07:05 +0100


"David Lindauer" <camille@bluegrass.net> wrote in message
news:417ACB1B.F20CCAA9@bluegrass.net...
>
>
> JKop wrote:
>
>> Let's say you've a very simple function, that, if it fails, should throw
>> an
>> exception. The thing is though, it's not important enough to go and
>> actually
>> define an "exception class" for, so... is there any general exception
>> class
>> defined in the Standard Library that I could use in such circumstances,
>> maybe something like:
>>
>
> for example:
>
> #include <stdexcept>
>
> using namespace std ;
>
> void myfunc(bool bad_things_happen)
> {
> if (bad_things_happen)
> throw runtime_error("my message here") ; // use a runtime_error
> since
> you can't associate a
>
> //message
> with the standard 'exception' class
> }
> int main(int argc, char **argv)
> {
> try {
> myfunc(true) ;
> } catch (runtime_error &aa) { // note you could catch 'exception' here
> if you
> want to catch all the
> // exceptions in stdexcept
> cout << aa.what() ; // prints your message
> }
> }

Wow - I knew about the exception messages - but didn't know you could catch
them...
Can you catch <exception> errors too? Or is this just for <stdexcept> ?

-- 
=========
Comp Whizz
=========
(The C++ beginner)