Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does not return java function annotation

Tags:

java

throw

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.

like image 559
daniel Avatar asked Jan 19 '13 09:01

daniel


People also ask

How do you not return something in Java?

Note: Return statement not required (but can be used) for methods with return type void. We can use “return;” which means not return anything.

What can be returned from an annotation method declaration?

Annotations must return: an enum, primitive type or an annotation, String, or Class object. They can also return an array of these types.

What does Java annotation do?

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.

What is annotation used for?

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.


1 Answers

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();
}
like image 184
axtavt Avatar answered Oct 13 '22 15:10

axtavt