POV-Ray : Newsgroups : povray.off-topic : Standard libraries : Re: Standard libraries Server Time
6 Sep 2024 09:18:16 EDT (-0400)
  Re: Standard libraries  
From: Nicolas Alvarez
Date: 8 Mar 2009 20:00:44
Message: <49b45c2b@news.povray.org>
Nicolas Alvarez wrote:
> template<typename E>
> void my_throw_exception(E& exc) {
>     //do the backtrace() dance and store the results somewhere in exc
>     throw exc;
> }

And yes, you do need the template. The 'throw' statement uses the *static*
type of the variable. So for example:

class MyError: public std::exception {};

void my_throw_exception(std::exception& exc) {
    throw exc;
}

void foo(int test) {
    switch (test) {
    case 1:
        throw std::exception();
        break;
    case 2:
        throw MyError();
        break;
    case 3:
        my_throw_exception(std::exception());
        break;
    case 4:
        my_throw_exception(MyError()); // <- MyError gets sliced here
        break;
    }
}

int main() {
    try {
        foo(1);
    } catch(MyError& e) {
        std::cerr << "MyError thrown\n";
    } catch(std::exception& e) {
        std::cerr << "std::exception thrown\n";
    }
}

It will only print "MyError thrown" if you pass 2 to foo().


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.