ChapterÂ 10.Â Throw Your Own Errors
When I was younger, the most befuddling part of programming languages was the ability
to create errors. My first reaction to the
throw operator in Java
was, âWell, thatâs stupid; why would you ever want to
cause an error?â Errors were my enemyâsomething I
sought to avoidâso the ability to cause an error seemed like a useless and
dangerous aspect of the language. I thought it was dumb to include the same
first place. Now, with a great deal of experience under my belt, Iâm a big
fan of throwing my own errors.
The Nature of Errors
An error occurs in programming when something unexpected happens. Maybe the incorrect value was passed into a function or a mathematical operation had an invalid operand. Programming languages define a base set of rules that when deviated from, result in errors so that the developer can fix the code. Debugging would be nearly impossible if errors werenât thrown and reported back to you. If everything failed silently, it would take you a long time to notice that there was an issue in the first place, let alone isolate and fix it. Errors are the friends of developers, not ...