Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 6
Postcondition
The postcondition of String's charAt(int index) method is that its return value will be the character at position index and the string itself will remain unchanged.
If the client invokes charAt() and passes -1 or some value length() or greater, the client has broken the contract. In this case, the charAt() method can't do its job correctly, and it signals this to the client by throwing a StringIndexOutOfBoundsException. This exception indicates that the client has some kind of software bug or has not used the class correctly.
If the charAt() method finds that it has received good input (the client has kept its part of the bargain), but for some reason is unable
to return the character at the requested index (unable to fulfill its end of the contract), it would indicate this condition
by throwing an exception. Such an exception would indicate that the method has some kind of bug or difficulty with runtime
resources.
So, if an event represents an "abnormal condition" or a "broken contract," the thing to do in Java programs is to throw an exception.
Once you have decided to throw an exception, you need to decide which exception to throw. You can throw an instance of class
Throwable, or any subclass of Throwable. You can throw an already existing throwable object from the Java API, or define and throw one of your own. How do you decide?
Exceptions versus errors
In general, you should throw an exception and and never throw errors. Error, a subclass of Throwable, is intended for drastic problems, such as OutOfMemoryError, which would be reported by the JVM itself. On occasion an error, such as java.awt.AWTError, could be thrown by the Java API. In your code, however, you should restrict yourself to throwing exceptions (subclasses
of class Exception). Leave the errors to the big guys.
Checked vs Unchecked exceptions
The big question, then, is whether to throw a "checked" or an "unchecked" exception. A checked exception is some subclass
of Exception (or Exception itself), excluding class RuntimeException and its subclasses. Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked, but as you should be focusing on throwing exceptions only, your decision should be
whether to throw a subclass of RuntimeException (an unchecked exception) or some other subclass of Exception (a checked exception).
If you throw a checked exception (and don't catch it), you will need to declare the exception in your method's throws clause. Client programmers who wish to call your method will then need to either catch and handle the exception within the
body of their methods, or declare the exception in the throws clause of their methods. Making an exception checked forces
client programmers to deal with the possibility that the exception will be thrown.
If you throw an unchecked exception, client programmers can decide whether to catch or disregard the exception, just as with checked exceptions. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. Either way, client programmers are less likely to think about what they should do in the event of an unchecked exception than they are in the case of an checked exception.