There is a class for exception handling.

class VException : public std::exception { std::string name; public: VException(); VException(std::string); VException(std::string, std::exception&); std::string get_name() const; }; 

And when an exception is triggered in main, the program drops with the message terminate called after throwing an instance of 'char const*' It is clear that this is a problem when casting std::string to char const* , but it is not clear how to bypass it.

 Vector<int>* v; Vector<int>* v1; try { v = new Vector<int>; v1 = new Vector<int>(5); } catch(VException& ex) { std::cout << ex.get_name() << std::endl; } try { v->pop_back(); // тут срабатывает исключение удаления из пустого вектора } catch(VException& ex) { std::cout << ex.get_name().c_str() << std::endl; } 
  • I do not know what you understand, because I do not understand anything. Show your throw - ixSci
  • 3
    Vanguyu that you wrote throw "Ошибка" . The exception type turned out to be const char * , and was not caught by any handler. - yrHeTaTeJlb

1 answer 1

Handling exceptions is quite a subtle process, and std :: string is going to create problems for itself.

The point is that an exception can jump out of std :: string, and what will you do then?

Avoid using STL containers and dynamic memory when handling exceptions.

  • And why can std::string throw an exception? When you answer this question, answer another: what in this case can you do with the system? - ixSci
  • @ixSci when you express your wish regarding my answer, I will spend my time on making corrections. Rebus I do not do. - gbg
  • Yes, this is not a rebus, I just tried to point out to you the absurdity of the board, not to use std::string (and other dynamic containers) in exception classes. This is one of the tales, which has long been on the Internet, in which the meaning is almost 0. - ixSci
  • @ixSci have no other arguments? Links to examples, sources? An example of how to delete an exception from std :: string is easy, do at () outside the string and enjoy. A programmer just wanted to format the output beautifully. - gbg
  • yeah i [] outside of the C-line will this be normal? I didn’t quite understand what arguments you were expecting from me and what sources. You see, you are putting forward the thesis that the use of dynamic containers in exception classes can lead to problems, I do not argue with that, I only argue that these problems are irrelevant, since in principle, they cannot be solved in 99.999% of cases; therefore, such containers can be safely used in exception classes. - ixSci