+ 2

How to catch details of exception

Hi I have a code snippet where generic catch catch(...) is there. It is not giving me details and I don't know root cause also of exception. How to identify the error details? https://code.sololearn.com/cDmeRm6ieL50 In above code, I just have below and it works ok catch (...) { cout << "generic exception\n"; } but not getting what it throws. So added catch(exception& ex) but it is not handling at all. Removing generic block of exception and only keeping exception& one results into crash. Please suggest to get details of exception. I am on Visual Studio 2019 on windows environment. Thanks in advance!!!

16th Jul 2022, 1:01 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
11 Antworten
+ 3
The reason this is happening is because you dont throw an exception. To catch the exception you would have to throw an object inherited from std::exception
16th Jul 2022, 4:03 PM
SimZooo
SimZooo - avatar
+ 2
You can do catch(std::exception& e) and inside the catch block do std::cout << e.what();
16th Jul 2022, 3:53 PM
SimZooo
SimZooo - avatar
+ 1
type of the argument passed by the throw expression is checked against catch parameters, and only in the case they match, the exception is caught by that handler. try { throw "2"; //throw 2; } catch(const char *c){ cout << c <<endl; } try{ throw exception(); } catch (exception& ex) { cout << "exception\n" << ex.what(); }
16th Jul 2022, 5:24 PM
Jayakrishna 🇮🇳
+ 1
Thanks SimZooo and Jayakrishna🇮🇳 Got it now..... I have actually an exception caught by catch(...) and no other cacth blocks are there..... That exception might have occured in any functions out of 100s of functions.... Can i get any info like where and what happened ? Unfortunately I dont check call stack on debug mode as it is running on different machine not having Visual Studio installed
16th Jul 2022, 7:11 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
There's not much you can do unless you use a debugger such as the one in visual studio
16th Jul 2022, 8:12 PM
SimZooo
SimZooo - avatar
+ 1
Not unless you add that informatin to the constructor of the exception
16th Jul 2022, 8:40 PM
SimZooo
SimZooo - avatar
+ 1
You can use like this : try { throw runtime_error("error at line no: "+to_string(__LINE__) +"\nin file : " __FILE__); } catch(exception& ex) { cout << ex.what(); }
16th Jul 2022, 8:53 PM
Jayakrishna 🇮🇳
0
The block you have memtioned is already there in code snippet shared in question but it is not caught in that code block
16th Jul 2022, 4:03 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Ketan Lalcheta You can create an exception with a custom string, such as std::exception("{Error message} in file: " __FILE__ " at line:" __LINE__). When the exception is read with e.what(), it will output something like "Index out of bounds in file: main.cpp at line: 255.
16th Jul 2022, 7:19 PM
SimZooo
SimZooo - avatar
0
I don't have knowledge from where it is thrown There is huge code base and may have 100 cpp files and from that files , one function might be throwing this exception... Reason I am going into this matter is to identify the location where exception occurs
16th Jul 2022, 7:40 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I thought I can dump some information of the exception into text or some file as it is not replicated on system having debugger
16th Jul 2022, 8:35 PM
Ketan Lalcheta
Ketan Lalcheta - avatar