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
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
|