-
Why throw exceptions?
We're learning about throwing exceptions in Deitel's book. I get the concept ok, but don't see the value of it.
The typical example is catching an attempt to divide by zero. Why not just test for that, or whatever, in an if statement or similar. Maybe call another function if need be, without the bother of try, throw and catch? It seems more work than needed. I must be missing something, but what? Thanks.
-
Patience!
You will find situations later on that would be a little awkward to handle in other ways.
I'd give you an example, but I suspect it might confuse you!!! Wait until you understand dynamic class constructors, you'll find situations where exception handling will really simplify your coding.
Stick with understanding the concept - always good advice anyway.
-
Ok, you write a math library.. it does some division, and it checks for the 0 divide. The programmer made an error obviously, you shouldn't be able to divide by 0. So what do you do? Print an error message to console and quit the program? The purpose of exceptions is to decouple error finding and error handling.