Difference Between throw and throws Keyword in Java





In my previous post , I have explained about throw keyword here and throws keyword here.In this post I will discuss the difference between the two keywords.

The both serve different purpose. The difference table is given below:

Sl No throw throws
1 throw is used to create a new Exception object and throw it It is used in method definition to declare that a risky method is being called and the method may throw the specified Exception.
2 Using throw we can only throw one Exception type using throws we can declare multiple Exceptions
3 Example-throw new IOException("Can not open Connection"); Example- throws IOException,ArrayIndexBoudException
Difference Between throw and throws Keyword in Java Difference Between throw and throws Keyword in Java Reviewed by Animesh Chatterjee on October 04, 2018 Rating: 5

No comments:

Powered by Blogger.