user3369008
user3369008

Reputation: 101

Multiple string exceptions handling

I want to throw multiple (2) different string exceptions and I want them to be caught by 2 separate catches, how can I accomplish this?

    try
    {
        std::istringstream ss2(argv[i]);

        if (!(ss2 >> l)) throw (std::string)"abc";
        if(l < 0 || l > n) throw (std::string)"xyz";
    }

    catch(std::string abc) {
        do something for abc
    }

    catch(std::string xyz){
        do something for xyz
    }

Code above would always fire the first catch and never the second.

Upvotes: 2

Views: 354

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

The catch clause determines the exception to catch by type, not by value or an attribute of the exception object being caught.

If you would like to catch the two exceptions differently, you should create two separate exception types, rather than throwing std::string:

struct exception_one {
    std::string message;
    exception_one(const std::string& m) : message(m) {}
};
struct exception_two {
    std::string message;
    exception_two(const std::string& m) : message(m) {}
};
...
try {
    std::istringstream ss2(argv[i]);
    if (!(ss2 >> l)) throw exception_one("abc");
    if(l < 0 || l > n) throw exception_two("xyz");
}
catch(exception_one& abc) {
    do something for abc
}
catch(exception_two& xyz){
    do something for xyz
}

Note the ampersand & in the catch clause: you throw exceptions by value, and catch them by reference.

Upvotes: 3

Related Questions