Domanda

I'm new to exceptions and this is what I know so far:

  • throws and throw are different

  • doing throw IllegalArgumentException I'll probably have to add throws IllegalArgumentException to the method signature

  • IllegalArgumentException is an unchecked exception and "Exceptions whose handling is not verified during Compile time"

Sources: http://javarevisited.blogspot.com/2011/12/checked-vs-unchecked-exception-in-java.html#ixzz2yM5jNFeg, http://www.tutorialspoint.com/java/java_exceptions.htm

This is a homework question: does throwing IllegalArgumentException make the program quit instantly?

The question seems a bit general and likely to be false, but I'm not entirely clear about IllegalArgumentException either. The sources did not really exemplify throw in a simple way, so I'm still confused. So, it would be great if there could be a simple explanation to this.

È stato utile?

Soluzione

Throwing any exception makes the current method exit immediately, but not the whole program. Furthermore, the exception will continue being thrown at the calling method, from where the first method threw it; this is called propagation. Whether it keeps going at that point depends on whether that calling method catches the exception. If nothing catches the exception, it'll propagate all the way back to the main method (this is assuming a simple, single-threaded application), and if that method also throws it, then the program will exit.

void callee() {
    if (someTrueCondition()) {
        throw new IllegalArgumentException("whatever");
    }
    System.out.println("this will never get called");
}

void caller() {
    calle(); // the IllegalArgumentException propagates from here
    System.out.println("this will also never get called");
}

The above is true for any exception. What makes IllegalArgumentException different from others is only the fact that it's unchecked, and thus doesn't need to be declared in a throws clause on the method that may throw it. (Note that in your question, you said you probably would need to declare throws IllegalArgumentException, but you actually don't, because it's an unchecked exception.)

If you were instead throwing a checked exception (e.g., throw new SQLException()), then both callee and caller would need to declare that exception. callee would need to declare it because it throws that exception directly, and caller would need to declare it because it may throw that exception indirectly, via propagation from callee.

The only way to stop this propagation (both of the exception at runtime, and of the throws clauses at compile-time) is to catch the given exception with a try-catch.

Altri suggerimenti

An uncaught exception can make a single threaded application quit. If you were to drop the foo/bar example below into a Java app, you will see that the execution state will terminate because of an uncaught exception.

Difference between throws & throw

/**
 * This is my foo method
 * @throws IllegalArgumentException if argument is wrong!
 */
public void foo(String s) throws IllegalArgumentException {
    // Some code
    if (!s.equals("bar")) {
        throw IllegalArgumentException("something not quite right here...");
    }
}

You are nearly there - @throws is for documentation purposes in commenting as well as use in the method signature to indicate any possible exceptions a method might throw. This includes exceptions it directly or indirectly throws. It's always good to add any throws that it chains or doesn't catch that can be thrown from any methods it uses from the same or other objects/classes.

Throw is the implementation code. IllegalArgumentException will typically be thrown when you try to send the wrong class type of an argument to the method at run time. You can also manually throw them any time a check fails, as demonstrated above.

Also, you don't need to declare it in the method signature, however if you do declare it, any implementing code you have should be wrapped in a try/catch block.

An example of an indirect exception:

class foo {
    /**
     * @throws IllegalArgumentException when s != foo
     */
    public void foo(String s) throws IllegalArgumentException {
        if (!s.equals("foo")) {
            throws IllegalArgumentException("argument did not equal 'foo'");
        }
    }
}

class bar {
    protected foo foo;
    public bar() {
        this.foo = new foo();
    }

    public void bar(String s) throws IllegalArgumentException {
        this.foo.foo(s);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top