0

I'm working with this piece of code and I've found that the method doesn't throw the exception after a succesfull call. If I use std::cout all is fine and the exception is thrown. I'm using gcc version 4.9.2 (Debian 4.9.2-10). Is it a gcc bug or stl bug a code problem or what else?

// exceptions
#include <iostream>
using namespace std;
class C {
   public:
    string srch(int &i) {
        if (i == 0) {   //found
            wcout << "got it: " << i << endl; return "i";
        }
       throw std::exception();

    }

 };

 int main () {
   C c = C();
   int i = 2;
   int j = 0;
   try
   {
     c.srch(j);
     c.srch(i);
   }
   catch (const std::exception &e) {
     cout << "An exception occurred. Exception Nr. " << e.what() << '\n';
   }
    return 0;
 }

Here's an ideone link to reproduce the lack of an exception with wcout. and a link reproducing the exception when cout is used.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
blob
  • 439
  • 8
  • 21

1 Answers1

2

Your example does not prove the exception wasn't thrown.

The cout message in your catch block is not displayed, because you already used wcout and mixing character widths on the same device (stdout) is Undefined Behaviour.

Change the cout to wcout and you'll see the exception was thrown, you just didn't see the message you expected.

Useless
  • 64,155
  • 6
  • 88
  • 132