+ 3
What is the need of throws here.the program is working without throws also.
int div(int a, int b) throws ArithmeticException { if(b == 0) { throw new ArithmeticException("Division by Zero"); } else { return a / b; } }
4 Respostas
+ 4
throws a keyword which is used as definition of method it tells the program that this method is execpected to throw and catch and methods I don't know if it works on without it as b as 0
does that work
+ 2
In IDE's when you write your code, they usually show the exceptions for methods tagged with the 'throws' keyword. Since ArithmeticException is an unchecked exception, it would not be needed to use 'throws' for it, but this code would suggest, that you really should care about and calculate with ArithmeticException while using the given method. I think this is the same case as the @Deprecated annotation. It has no real purpose, but rather a warning.
But why can use.. ? I think, mainly because of syntax rules, the 'throws' keyword was written to be used with exception types after it, and this also matches. Similarly, the 'x = x;' codeline, it basically does nothing.
+ 1
I am not talking about throw.it throws
+ 1
do you have to use throws only when there is throw.