1

I have been learning c++ recently and while working on exceptions, I came across something that confuses me. I threw a string exception and added multiple catch blocks to see how it works, and for some reason, the string exception is being caught in the int catch block. This is puzzling me and I cant see to find reasons why it would do it.

FYI, I am using GCC v 5.3.0

So, I was expecting the following message on the console.

String exception occurred: Something else went wrong

But instead, I got the following message.

Int exception occurred: 14947880

So I tried switching the order of the catch blocks and placed the string catch block on the top, and I got the string exception message. Could somebody please explain a reason why this is happening? It seems whatever catch block is in the top is being caught first.

I don't think this is the correct behavior and that the string exception block should be executed, but if I am wrong, please correct me. Thanks for taking time to look at this.

#include <iostream>
using namespace std;

void MightGoWrong()
{
    bool error = true;
    if (error)
    {
        throw string("Something else went wrong");
    }
}

/**
 * Output on console: Int exception occurred: 14947880
 */
int main() {
    try
    {
        MightGoWrong();
    }
    // If string or char pointer catch block goes to top, that block will execute
    catch (int e)  
    {
        cout << "Int exception occurred: " << e << endl;
    }
    catch (string &e)
    {
        cout << "String exception occurred: " << e << endl;
    }
    catch (char const * e)
    {
        cout << "Char exception occurred: " << e << endl;
    }
    return 0;
}
J. Lee
  • 513
  • 4
  • 15
  • [Works for me](http://ideone.com/FWUnsq) with gcc - what compiler are you using ? – Paul R Mar 23 '17 at 08:57
  • [Cannot reproduce](http://ideone.com/rzkAaG). – Algirdas Preidžius Mar 23 '17 at 08:58
  • Works [here](http://coliru.stacked-crooked.com/a/804ac69f8bab9bf9) with gcc/clang. – Jarod42 Mar 23 '17 at 09:00
  • I am using GCC v 5.3.0. Hmmm i see. There must be something up with my compiler – J. Lee Mar 23 '17 at 09:00
  • 4
    Or you are not running the code you posted... – Mats Petersson Mar 23 '17 at 09:01
  • I tried cleaning and rebuilding multiple times but i am still getting the same results – J. Lee Mar 23 '17 at 09:05
  • try throwing a float and have a float catch block right on top. What happens? – pcodex Mar 23 '17 at 09:07
  • 1
    What about if you add `#include `? – Anto Jurković Mar 23 '17 at 09:08
  • @Prab I tried throwing a float error with the float catch block on the top and it is going to the float catch block. Once i move the float catch block below the int block, it is then going to the int catch block. – J. Lee Mar 23 '17 at 09:12
  • @AntoJurković I just tried adding the include string preprocessor statement, and the result is still the same. Still executing the first catch block no matter what. – J. Lee Mar 23 '17 at 09:14
  • Possible duplicate of [Do I have to use #include beside ?](http://stackoverflow.com/questions/16506095/do-i-have-to-use-include-string-beside-iostream) – Anto Jurković Mar 23 '17 at 09:14
  • 3
    @J.Lee that should not happen. Whether you have the float catch block after or before the int catch block control will flow to the float catch block if you throw a float exception. There's definitely something amiss with your environment. btw I asked you to try the float case just to prove that it's not just the string case which is not working – pcodex Mar 23 '17 at 09:16
  • @prab. Thank you for the clarification. I was guessing that something was wrong with my environment. I guess i ll start taking a look at my compiler settings. Thank you so much for the help! – J. Lee Mar 23 '17 at 09:22
  • 1
    I just tried running the same code using the Visual C++ compiler (as opposed to my GCC) and its working as expected. There is definitely something funky with the GCC settings on my localhost. Thank you everybody for the help! – J. Lee Mar 23 '17 at 09:42

0 Answers0