A
Andreas Leitgeb
May be a wish to the easter bunny, or I might just have missed it
so far (like some easter egg)
Suppose I have some "doCleanUpAndExit" method, that in the
end either calls System.exit() or throws some Error or custom
RuntimeException.
I'd like the compiler to *know* that this method cannot possibly
return, and that any code following it should be considered
as "unreachable" code by the compiler.
E.g., I'd have code like:
class Foo {
@DoesntReturn // tell compiler this method isn't supposed to return
public void doCleanUpAndExit() {
// compiler would barf if there was any "return"
// or possible flow to end of method.
throw new ThisIsTheEndException();
// or System.exit(); which would itself need to be thusly flagged.
}
// test1() and test2() are boolean-returning methods...
public boolean check() {
if ( ! test1() ) { // aww, it's all borked up
doCleanUpAndExit();
// any further code here would make the compiler barf
} else {
return ( test2() );
}
// compiler should know that flow won't ever arrive here,
// so there is no way that the method doesn't return a boolean..
}
}
Did I miss some annotation that already does this?
Is there anything like this already in the queue for
future Java versions?
Thanks in advance.
PS: I've already defined a "WontEverGetHereException" to throw
at certain places, where the compiler otherwise barfed about
possible non-returning exits from value returning methods, so
I'm rather asking out of curiosity for nicer solutions than
facing a real problem. Still I'd rather have it a compile-time
barf than a run-time barf if my assumptions were wrong w.r.t
possible flow...
PPS: any enlightenment about why this could be wrong-headed would
also be appreciated, except if it boils down to pointing out the
intended "exceptional" semantics of exceptions... Calling those
non-returning methods itself would only happen exceptionally.
so far (like some easter egg)
Suppose I have some "doCleanUpAndExit" method, that in the
end either calls System.exit() or throws some Error or custom
RuntimeException.
I'd like the compiler to *know* that this method cannot possibly
return, and that any code following it should be considered
as "unreachable" code by the compiler.
E.g., I'd have code like:
class Foo {
@DoesntReturn // tell compiler this method isn't supposed to return
public void doCleanUpAndExit() {
// compiler would barf if there was any "return"
// or possible flow to end of method.
throw new ThisIsTheEndException();
// or System.exit(); which would itself need to be thusly flagged.
}
// test1() and test2() are boolean-returning methods...
public boolean check() {
if ( ! test1() ) { // aww, it's all borked up
doCleanUpAndExit();
// any further code here would make the compiler barf
} else {
return ( test2() );
}
// compiler should know that flow won't ever arrive here,
// so there is no way that the method doesn't return a boolean..
}
}
Did I miss some annotation that already does this?
Is there anything like this already in the queue for
future Java versions?
Thanks in advance.
PS: I've already defined a "WontEverGetHereException" to throw
at certain places, where the compiler otherwise barfed about
possible non-returning exits from value returning methods, so
I'm rather asking out of curiosity for nicer solutions than
facing a real problem. Still I'd rather have it a compile-time
barf than a run-time barf if my assumptions were wrong w.r.t
possible flow...
PPS: any enlightenment about why this could be wrong-headed would
also be appreciated, except if it boils down to pointing out the
intended "exceptional" semantics of exceptions... Calling those
non-returning methods itself would only happen exceptionally.