Is there any way to force the compiler (annotation or othewise) to realize a java function is never returning (i.e. always throwing), so that subsequently it will not error out its usages as the last statement in other functions returning non void?
Here's a simplified/made-up example:
int add( int x, int y ) {
throwNotImplemented(); // compiler error here: no return value.
}
// How can I annotate (or change) this function, so compiling add will not yield
// an error since this function always throws?
void throwNotImplemented() {
... some stuff here (generally logging, sometimes recovery, etc)
throw new NotImplementedException();
}
Thank you.
Note: Return statement not required (but can be used) for methods with return type void. We can use “return;” which means not return anything.
Annotations must return: an enum, primitive type or an annotation, String, or Class object. They can also return an array of these types.
Java annotations are metadata (data about data) for our program source code. They provide additional information about the program to the compiler but are not part of the program itself. These annotations do not affect the execution of the compiled program.
Annotating is any action that deliberately interacts with a text to enhance the reader's understanding of, recall of, and reaction to the text. Sometimes called "close reading," annotating usually involves highlighting or underlining key pieces of text and making notes in the margins of the text.
No, it's not possible.
Note, however, that you can easily work it around as follows:
int add( int x, int y ) {
throw notImplemented();
}
Exception notImplemented() {
... some stuff here (generally logging, sometimes recovery, etc)
return new NotImplementedException();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With