visual c++ - a small issue in using exception handling in c++ -
the below given simple exception program giving unhandled exception "microsoft c++ exception: int @ memory location 0x0012fe94..". getting error after function excep() returned. please can tell why error coming here. helpful if possible mistakes in code explained/analysed. learning code better.
i using visual c++ 2005.
#include <iostream> using namespace std; int main () { int excep(int); throw excep(20); return 0; } int excep(int e) { cout << "an exception occurred. exception nr. " << e << endl; return 0; }
in line
throw excep(20);
excep(20)
called first, return value thrown, i.e. throw integer. equivalent to:
const int = excep(20); throw i;
to idea how exception-snytax looks:
#include <iostream> #include <stdexcept> using namespace std; int main () { try { // throw std::runtime_error("test"); } catch (const std::exception &e) { std::cout << "exception: " << e.what() << std::endl; } }
in practive: never throw not derived std::exception
.
proposed readings:
- introductory book on c++ (search stackoverflow this)
- c++ faq on exception handling
- "exceptional c++" (for advanced c++ users)
Comments
Post a Comment