+ 2

C++

how is this code works? especially throw Ex("mistake"); #include <iostream> class Ex{ std::string message; public: Ex(const std::string msg) : message(msg){} std::string yaz(){ return message; } }; int main() { int a = 2; try{ if(a<3) throw Ex("mistake"); std::cout<<a + 3; } catch(Ex err){ std::cout<<err.yaz(); } return 0; }

3rd Sep 2022, 8:14 PM
kullanici 8
kullanici 8 - avatar
2 Answers
+ 3
Ex("mistake") is an object of class Ex with message set to "mistake" You are throwing this object and catching by catch(Ex err), in that catch block, calling yaz() function which returns message
3rd Sep 2022, 8:37 PM
Jayakrishna 🇼🇳