vendredi 28 juillet 2017

strange behaviour of this c++ piece of code (std::wcout and std::exception)

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;
 }

Aucun commentaire:

Enregistrer un commentaire