Java Reference
In-Depth Information
found : java.lang.Throwable, required: T
throw (T) t;
^
A warning is the compiler's way of telling you that you may be shooting yourself in the foot, and in
fact you are. The unchecked cast warning tells you that the cast in question will not be checked at
run time. When you get an unchecked cast warning, modify your program to eliminate it, or
convince yourself that the cast cannot fail . If you don't, some other cast may fail at an
undetermined time in the future, and you may have a hard time tracing the error to its source. In this
case, it's even worse: The exception that is thrown at run time may not conform to the signature of
the method. The sneakyThrow2 method exploits this methodically.
There are several lessons for platform designers. When designing libraries that are implemented
outside the language, such as the reflection library, preserve all guarantees made by the language.
When designing from scratch a platform that supports generic types, consider enforcing their
correctness at run time. The designers of Java's generic typing facility did not have this luxury, as
they were constrained by the requirement that generified libraries interoperate with existing clients.
To eliminate the possibility of an exception that violates a method's signature, consider enforcing
exception checking at run time.
In summary, Java's exception checking is not enforced by the virtual machine . It is a compile-
time facility designed to make it easier to write correct programs, but it can be circumvented at run
time. To reduce your exposure, do not ignore compiler warnings.
< Day Day Up >
 
 
Search WWH ::




Custom Search