+ 5
What is the difference between throw & throws in Java ??
2 odpowiedzi
+ 8
You use throw in order to throw a new exception.
For example:if you're method should only accept positive arguments, it could throw an exception, it doesn't calculate anything and the user has to implement a catch:
public void foo (int i){if i < 0
throw new IllegalArgumentException ();}
you haveuse throws if there is the possibility that ure method throws a non-runtime exception; so it has to be caught.it is part of the signature.
public void foo(Path p) throws IOException{
BufferedReader bf=Files.newBufferedReader (p)}
+ 4
the difference between throw & throws is that;
throw keyword is used to rethrow the exception after catching it .
wherever throws say that I can't handle that exception, send it to the parent function.