Difference between throw and throws keyword on Exception handling in Java is a popular core java interview question. Exception handling being an important part of Java programming language, complete knowledge of all keywords related to exception handling e.g. try, catch, finally, throw and throws is important. Main difference between throw and throws is in there usage and functionality. where throws is used in method 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 its throwing IOException. 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:
Difference between throw and throws in Exception handling - Java Example
signature a method suggesting its caller to prepare for Exception declared in throws clause, specially in case of checked Exception and provide sufficient handling of them. On the other hand throw 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:
worth remembering is that static initalizer block should complete normally.
How to Convert Map into List in Java
Difference between Thread and Runnable in Java
How to parse XML Files in Java using DOM parser
How to use Factory Design pattern in Java
How to find current Date in Java with Example
Quick tip to Convert Enum to String in Java
Difference between ArrayList and LinkedList in Java
No comments:
Post a Comment