Difference between throw and throws
keyword on Exception handling inJava is a
popular core java interview question
. Exception handling being an important part of Java programming language,complete knowledge of allkeywords related to exception handling e.g.try, catch, finally, throw andthrows is important. Main difference between throw and throws is in there usage and functionality. where throws is used inmethod signature to declare Exception possibly thrown by any method, throw is actually used to throw Exception in Java code, here is an example of both throw and throws keyword which makes it
easy to understand difference between them.
if you see the code above, throws keyword is used to declare Exception thrown by this method
shutdwon(),
by looking at method signature we know that itsthrowing
IOE
x
ception
. while inside method code throw keyword is actually used to throw instance of IOException. here are couple of more differences between throw and throws keyword in java:
Read more: http://javarevisited.blogspot.com/2012/02/difference-between-throw-and-throws-in.html#ixzz2ki7O1RWI
public void shutdown() throwsIOException{
throw new IOException("Unable to shutdown");
}

Difference between throw and throws in Exception handling - Java Example
1) You can declare multiple exception thrown by method in throws keyword byseparating them in common e.g. throws IOException, ArrayIndexBoundException etc, while you can only throw one instance of exception using throw keyword e.g.
throw new IOException("not able to open connection").
2) throws keyword gives a method flexibility of throwing an Exception rather than handling it. with throws keyword in method
signature a method suggesting its caller to prepare forException
declared in throws clause, specially in case ofchecked Exception
and provide sufficient handling of them. On the other handthrow keyword transfer control of execution to caller by throwing an instance of Exception. throw keyword can also be used in place of return as shown in below example:
private static boolean shutdown() {
throw new UnsupportedOperationException("Not yet implemented");
}
as in below method shutdown should return boolean but having throw in place compiler understand that this method will alwaysthrow exception .
3) throws keyword cannot be used anywhere exception method signature whilethrow keyword can be used inside method or static initializer block provided sufficient exception handling as shown in example.
static{
try {
throw new Exception("Not able to initialized");
} catch (Exception ex) {
Logger.getLogger(ExceptionTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
4) throw keyword can also be used to break a switch statement without using break keyword as shown in below example:
int number = 5;
switch(number){
case 1:
throw new RuntimeException("Exception number 1");
case 2:
throw new RuntimeException("Exception number 2");
}
We have seen differences between throw and throws in Java and they should be used accordingly. Let me know if you come across any otherdifference on throw vs throws in java.
Read more: http://javarevisited.blogspot.com/2012/02/difference-between-throw-and-throws-in.html#ixzz2ki7O1RWI