Spaidafora
PRO
last week
Spaidaforacountry asked

We need a throw for the catch to work?

N
Expert
3 days ago
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.