1

Could someone explain me when it is useful to use the keyword throw new .. instead of using throws next to the signature of a method ?

I know that when a method throws a Checked Exception. Java forces us to deal with it by doing it directly in the method by handling the Exception into a try-catch bloc or by specifying that it will be done elsewhere with the keyword throws next to the signature.

However, I have some trouble to understand when it is useful to use the keyword throw new and why. Is it related with handling Unchecked Exceptions ?

For instance in this example. Why don't we throw new ArithmeticException() in the method compute ? Since ArithmeticException is an unchecked one ? Should we not add something like :

private static int compute(int i) { if(i == 0) { throw new ArithmeticException(); } return 1/i; } 

.

public class Main { public static void main(String[] args) { for (int i = -2; i < 2; i++) { try { System.out.println(i+" -> "+compute(i)); } catch (ArithmeticException e) { System.out.println(i+" -> undefined") } } } private static int compute(int i) { return 1/i; } 

}

1 Answer 1

7

throws tells others that this method can throw an exception. Think of it as documentation. Checked exceptions must be part of a method's signature.

throw actually throws the exception. (throw new Exception(); first creates a new exception instance and then throws that instance. You could use two separate statements: Exception ex = new Exception(); throw ex;) You can throw both checked and unchecked exceptions with the throw keyword.

void checked() throws Exception { // required, Exception is a checked exception throw new Exception(); } void unchecked() { throw new RuntimeException(); // throws not required, RuntimeException is not checked } void checkedCall() throws Exception { checked(); // throws required, because "checked" can throw a checked exception } void caught() { try { checked(); } catch (final Exception ex) { // throws is no longer required, because the (checked) exception is handled within the method itself. It never leaves the method. } } 

Regarding your updated question: you don't throw new ArithmeticException, because it is thrown by the JVM when it tries to apply the / operator. You can throw it manually if you want to, but that's just redundant code.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your fast answer ! However, when is it necessary/useful to use only throws and sometimes throws and throw? I have seen some exemples where only throws was used ans sometimes both and I am not able to figure out why. You can maybe show me an example.
@Student123 throws is required if your method (or any methods it calls) can throw a checked exception. I have added code examples
Yes but what about throw new ? I don't understand when it is useful to use it. For instance, in the method checked() can we remove the keyword throw new Exception() ?
@Student123 what do you mean with "useful"? When your logic encounters an exceptional state, you usually want to throw an exception to signal the error to the caller(s). Say you have a method to download a file – what if your network connection is down? Or you are trying to read a file from disk: what if the file does not exist? Those are cases were it could make sense to throw an exception.
I added some code :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.