+ 5

What is the difference between throw & throws in Java ??

27th Oct 2016, 1:29 PM
Sambit Mohapatra
Sambit Mohapatra - avatar
2 Respuestas
+ 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)}
27th Oct 2016, 7:48 PM
No One
No One - avatar
+ 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.
22nd Jan 2017, 6:58 PM
Prachi Sahu
Prachi Sahu - avatar