1

Some methods are deciared this way: public int myMethod() throws Exception { (return an int or throw Exception if method fails). However, stub methods in NetBeans look like

public class JavaApp {
    public static void main(String[] args) {        
        if(false){ //this branch is never used
                int a = getInt();
        }
    }

    private static int getInt() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

This stub doesn't attempt to return an int. Instead it throws exception right away. Yet, IDE accepts it as a valid syntax. The program compiles and runs.

Is throw new UnsupportedOperationException considered a valid return for any return type? What is the role of throws Exception in the definition of a Java method?

4

What is the role of “throws exeption”

It's used for checked exceptions.

The idea is, if you write code to call a function that throws FoobarException, then the compiler will force you to either handle the exception, or to add the same throws FoobarException declaration to the function that you are writing.

It's a way to make sure that nobody will forget that a FoobarException might be thrown.

This stub doesn't attempt to return an int. Instead it throws exception right away. Yet, IDE accepts it as a valid syntax. The program compiles and runs.

Checked exceptions turn out not to be as popular as the language designers had hoped. In fact, they anticipated that when they created a whole category of unchecked exceptions.

UnsupportedOperationException is an unchecked exception. Unchecked exceptions work exactly the same as checked exceptions. The only difference is that you are not required to declare the exception when you write a method that could throw it. Every exception that inherits from java.lang.RuntimeException is unchecked.

The idea is, you're supposed to use checked exceptions for conditions that your program ought to handle in some graceful way (e.g., inform the user about a file-not-found instead of just crashing), and only use unchecked for exceptions that "can't possibly happen" unless your program is broken.

Is throw new UnsupportedOperationException considered a valid return for any return type?

No, it's not a "return", it's an exception. The compiler doesn't mind that your method never returns an int, because the compiler can tell that your method never returns at all.

2

It doesn't say much in the Javadocs about how this is implemented in the compiler, nor does it for the equivalent NotImplementedException in C#.

However, it wouldn't surprise me if the compiler's static code analysis went something like this: an exception is thrown unconditionally before return is called, so there's no need to check the return type.

When you think about it, this does make sense. The only reason these exceptions exist is to allow you to stub out a method for later implementation; it would be quite inconvenient if you couldn't specify a return type.

It would be easy enough to find out if the compiler treats this particular exception as a special case. Just change it to some other exception, and see if the compiler complains.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.