what() function of an exception

How can I set the content that is output by the what() function of the exception?

1
2
exception ex("Content of what()"); //I can NOT do this
cout<<ex.what();
Last edited on
Why not? It works for me. What compiler are you using?
I am using "g++ (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)". The problem is this

1
2
3
4
5
6
7
8
9
10
11
main.cpp: In function ‘int main()’:
main.cpp:6:37: error: no matching function for call to ‘std::exception::exception(const char [18])’
main.cpp:6:37: note: candidates are:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/ios:40:0,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/ostream:40,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/iostream:40,
                 from main.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/exception:65:5: note: std::exception::exception()
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/exception:65:5: note:   candidate expects 0 arguments, 1 provided
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/exception:62:9: note: std::exception::exception(const std::exception&)
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/exception:62:9: note:   no known conversion for argument 1 from ‘const char [18]’ to ‘const std::exception&’


The full program is

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <exception>
using namespace std;

int main()
{
    exception ex("Content of what()"); //I can NOT do this
    cout<<ex.what();
   return 0;
}


I tried also to #include <exception> but it wont help.


Last edited on
You can use something other than exception. Try something that extends exception?
http://www.cplusplus.com/reference/exception/
I notice logic_error, runtime_error, bad_alloc, etc.
These classes extend the base class of exception and define the virtual what method.
Could you please give me some example howto "fill" the what() function?
I just solved it:-). It looks like I forgot to #include <stdexcept>. Also I should have used logic_error like kevinjkt2000 said.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <stdexcept>
using namespace std;

int main()
{
    logic_error ex("Content of what()");
    cout<<ex.what();
   return 0;
}
Last edited on
Just to resolve your issue, the problem is that std::exception is a base class, with virtual functions. You should never instantiate an exception directly. Either derive your own class(es) from it and use those, or use one of the standard exception classes.

Personal nit-pick -- I think you should probably be throwing a std::runtime_error.

Hope this helps.
Personal nit-pick -- I think you should probably be throwing a std::runtime_error.


Wait, so you're saying it's a logic error that he's using a std::logic_error?

I feel dizzy.
Yes, he should have known better before doing it.




LOL
It works for me
Topic archived. No new replies allowed.