C ++ exception - line throw

I have a little problem with my code. For some reason, when I try to pass the line with the code below, I get an error message in visual studio.

#include <string> #include <iostream> using namespace std; int main() { char input; cout << "\n\nWould you like to input? (y/n): "; cin >> input; input = tolower(input); try { if (input != 'y') { throw ("exception ! error"); } } catch (string e) { cout << e << endl; } } 

Error:

error

Link: http://i.imgur.com/pYAXLuU.png

+6
source share
2 answers

you are currently casting const char* , not std::string , you should throw string("error") instead

edit: error resolved with

 throw string("exception ! error"); 
+10
source

Throwing a line is a really bad idea.

Feel free to define your own exception class and have a line embedded inside it (or just get your own exception class from std::runtime_error , pass the constructor error message and use the what() method to get the error line on the catch node), but do not output the line !

+9
source

Source: https://habr.com/ru/post/978810/


All Articles