|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.lang.Throwable | +--java.lang.Exception | +--java.lang.RuntimeException | +--lava.UnreachableCodeException
Indicates unreachable execution path.
Details: An UnreachableCodeException
is thrown when an execution path that the programmer has assumed to be unreachable actually turns out to be reachable. Therefore, ideally, this exception should never be thrown, and if it is, it indicates a flaw in the programmer's thinking -- but this is better than allowing the flaw to go undetected!
As an extra safeguard to your program, always be sure to throw this exception wherever you have assumed that code is unreachable. This will help you debug your program later if it turns out that you were wrong.
Example:
Your code switch
es on the variable shape
. You know, in this particular switch
statement, that shape can be only one of three values. Your code might look like this:
int sides; switch (shape) { case TRIANGLE: sides = 3; break; case SQUARE: sides = 4; break; case PENTAGON: sides = 5; break; default: // We know that shape must have been one of // the three values mentioned above, but we're // putting a catch-all in here, just in case. throw new lava.UnreachableCodeException (); } return sides;
In this example, adding the unreachable default
case not only guarantees that you'll be notified if your assumption is wrong, but, in this case, allows the compiler to process your code without complaining that sides
might not have been initialized. (You see, compilers just aren't as smart as humans!)
There are times when an UnreachableCodeException
should be used just to fool the compiler:
Example:
The method foo
divides a value by two if the value is even, but terminates the program if the value is odd.
int foo (int n) { if (n % 2 == 0) return n / 2; System.exit (0); // exit() will never return, but does the compiler // know that? No! It will complain about needing // a return value right here. }
Although this code looks correct, because the exit
method does not return, the compiler will not accept it. This is because the compiler is not aware that the call to System.exit
will never return. Instead, it expects a return value at the end of the method. This problem is easily remedied, in a self-documenting way, by using UnreachableCodeException
.
int foo (int n) { if (n % 2 == 0) return n / 2; System . exit (0); throw new lava.UnreachableCodeException (); }
The compiler will now accept this code. Although you could have inserted a dummy return
statement into the code, the approach shown here renders the code more "self-documenting," and guarantees that you'll be notified if your assumption about System.exit
ever turns out to be false
(for some strange reason).
Constructor Summary | |
UnreachableCodeException()
Initializes without message. |
|
UnreachableCodeException(java.lang.String s)
Initializes with message. |
Methods inherited from class java.lang.Throwable |
fillInStackTrace, getLocalizedMessage, getMessage, printStackTrace, printStackTrace, printStackTrace, toString |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public UnreachableCodeException()
Details: This default constructor initializes a new UnreachableCodeException
without an exception message.
public UnreachableCodeException(java.lang.String s)
Details: This constructor initializes a new UnreachableCodeException
with the given exception message.
s
- the messsage
|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |