Search code examples
c++exceptionthrow

Is it possible to throw two values at the same time?


I have 4-line and two throw statements ;

Pseudocode ;
In function f () 
     if a == 2 
           throw SMT_0 
     if b == 3  
           throw SMT_1 

For a != 2 and b != 3, I want to throw both throw statements, simultaneously. How can I do ?

 ex : 
     if a!= 2 && b != 3 
            throw SMT_0 and SMT_1

Solution

  • You cannot. But you can throw any object, so you can use OO techniques to have the one object you throw contain the relevant information from both SMT_0 and SMT_1.

    What are the thrown values, and what does your catch block look like?

    Update:

    OK, so it looks like you want to throw the reason(s) that your input was not correct.

    Question: why? Is it that important that all of the reasons have to be reported? Maybe you can simply throw reporting one of the reasons, let the user fix that and iterate? (If the intended recipient of the information is not the user but instead the programmer, I see absolutely no reason that all of the errors have to be reported at once).

    Now, if you do have to throw reporting all errors, then you can simply throw an array of enum CLASSNAME.REASONS instead of one value.