PRO
Spaidafora
asked
N
Expert
Nisha Sharma answered
Yes. A catch block only runs if something is thrown inside the try block.
In your example, throw 0; is what triggers the exception. When denominator == 0, the program throws 0, skips the remaining lines in the try block, and jumps straight to:
catch (int num) { ... }If the denominator isn’t 0, nothing gets thrown, so the catch block is skipped and the program continues normally.
Feel free to reach out if you have any more queries.
C++
This question was asked as part of the Learn C++ Intermediate course.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer
