Exceptions

Introduction

Exceptions are a means of communicating between methods / classes. What happens if a hammer hits an orange? Juice flies everywhere - an exception occurs and the juice is thrown => the exception is thrown and must b caught. Try the process first and hopefully the hammer will not hit the orange!

Consider the following analogy: My manager gives me a task to perform. I, in my turn, give the job to a colleague. Instructions as to what to do are written down and handed to my colleague - the task by the way is to fix the coffee machine. Coffee machines being complicated technology occasionally require parts to be replaced, for which such parts is supplied with the repair instructions. My colleague proceeds with the instructions, but finds that a part required for the repair is missing. What should he do? He can attempt to fix the machine himself - he may be able to use a different part. Otherwise, he will shout "Problem" and throw the problem back to me - it becomes my responsibility now. What should I do? I might be able to supply the spare part and fix the problem myself. I might not even try, as seriously, I don't know anything about coffee machines and automatically throw the problem back to my superior. I won't just throw the problem back to my manager, but will include some information - i.e. a particular spare part needs to be ordered, which he can authorise - in other words the exception acts as a problem report. Thus, we can see that an exception resembles an unexpected problem. The problem can be fixed by the relevant method, or else it can be passed back the method and class hierarchy.