Close to leaving, I decided to make up for the knowledge of C++11 that I had never learned before. Suddenly, I turned to exception handling. I felt it was a little fun and wrote a test program myself. Then the three views were completely subverted.
The source code is as follows:
#include <iostream> #include <string> #include <exception> void speak(int i) { if(i <= 0) { throw "System get a wrong..."; } } void main() { try { speak(-1); } catch(std::exception e) { std::cerr << "exception info:" << e.what() << std::endl; } }
It's very simple. It's right. It's also right to compile. Then it runs and hangs. Through the Debug trace, it's found that such a line hangs
catch(std::exception e), and then all kinds of doubts, all kinds of changes, it's just a matter of reloading the system.
Unintentionally changed this sentence, and then handled the exception by yourself. The changed code is as follows:
#include <iostream> #include <string> #include <exception> void speak(int i) { if(i <= 0) { throw std::exception("System get a wrong..."); } } void main() { try { speak(-1); } catch(std::exception e) { std::cerr << "exception info:" << e.what() << std::endl; } }
Pay attention to throw. It's silly. Maybe exception is an explicit class. There's not much Kung Fu research. Just remember. The reason why the program hangs up may be that it can't be captured here, and it is directly handed over to the operating system.
==================Supplementary notes===================
I posted the statement of the exception class I said in the morning below. I ran into problems under different platforms:
_STDEXT_BEGIN class exception { // base of all library exceptions public: static _STD _Prhand _Set_raise_handler(_STD _Prhand _Pnew) { // register a handler for _Raise calls const _STD _Prhand _Pold = _STD _Raise_handler; _STD _Raise_handler = _Pnew; return (_Pold); } ====Here's why char *Cannot automatically convert to exception Why? // this constructor is necessary to compile // successfully header new for _HAS_EXCEPTIONS==0 scenario explicit __CLR_OR_THIS_CALL exception(const char *_Message = _MESG("unknown"), int x=1) _THROW0() : _Ptr(_Message) { // construct from message string (void)x; } __CLR_OR_THIS_CALL exception(const exception& _Right) _THROW0() : _Ptr(_Right._Ptr) { // construct by copying _Right } exception& __CLR_OR_THIS_CALL operator=(const exception& _Right) _THROW0() { // assign _Right _Ptr = _Right._Ptr; return (*this); } virtual __CLR_OR_THIS_CALL ~exception() { // destroy the object } virtual const char * __CLR_OR_THIS_CALL what() const _THROW0() { // return pointer to message string return (_Ptr != 0 ? _Ptr : "unknown exception"); } void __CLR_OR_THIS_CALL _Raise() const { // raise the exception if (_STD _Raise_handler != 0) (*_STD _Raise_handler)(*this); // call raise handler if present _Doraise(); // call the protected virtual _RAISE(*this); // raise this exception } protected: virtual void __CLR_OR_THIS_CALL _Doraise() const { // perform class-specific exception handling } protected: const char *_Ptr; // the message pointer }; _STDEXT_END
Another problem I have encountered is that in gunc, exception does not have a constructor with char. To construct an exception through char, only the subclass with this constructor can be used. However, on windows platform, exception has this constructor. Just remember here.