I understand that try
and catch()
are used for exception handling, just in case an error or crash would occur in the program under certain cases. I also understand how they work. But why use try
and catch()
? Why not just use an if()
statement that looks for a certain case and if that case is true, it does cout << //error code
?
Exception handling:
X
- supports notation x1 = x2 + x3
- where could an error code be returned?try...catch
does more. It unwinds the stack which calls the destructors for all automatically allocated objects since the try
was entered. If you do it your suggested way you'll have to keep track of these object manually or you'll get memory issues (leaks, overwriting, stale pointers, double deletes)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With