Good all the time of day, gradually mastering C ++ and came across the topic of exceptions, began to rummage through the Internet, found several explanations that confused me even more. Please tell me how to handle exceptions correctly in C ++ and provide a couple of examples or skip links where there are good easy examples for understanding, thanks in advance)

    2 answers 2

    Here is such a simple example for initial understanding:

    class exception; // класс ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ try { ... throw exception(); // [1] гСнСрация ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ exception ... throw new exception(); // [2] гСнСрация ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ exception* } catch(exception e) { // Π›ΠΎΠ²ΠΈΠΌ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ [1] } catch(const exception& e) { // Π›ΠΎΠ²ΠΈΠΌ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ [1] (Ρ‚ΠΎΠΆΠ΅ самоС, Ρ‡Ρ‚ΠΎ ΠΈ ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰ΠΈΠΉ ΠΈ Ρ€Π΅ΠΊΠΎΠΌΠ΅Π½Π΄ΡƒΠ΅ΠΌΡ‹ΠΉ(!!!) способ ΠΎΡ‚Π»ΠΎΠ²Π° ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ) } catch(exception* p) { // Π›ΠΎΠ²ΠΈΠΌ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ [2] } 
    • Thank you, if you can skip links to real examples for clarity - Egor Sokolov
    • @ Yegor Sokolov, there are no examples at hand, can anyone else help or look in Google itself. - IAZ

    the main thing is to try smaller, and it is better to completely eliminate the use of the so-called catch evething - interception

     catch(...) { } 

    it's a bad style