What is difference between throws and throw? | SoloLearn: Learn to code for FREE!

+1

What is difference between throws and throw?

5/15/2020 8:45:03 PM

[email protected]!¶∆

2 Answers

New Answer

+2

1. throws keyword is used to declare an exception with the method name. It works like the try-catch block because the caller needs to handle the exception thrown by throws. On the other hand, throw keyword is used to throw an exception explicitly. 2. throw is followed by an instance of Exception class and throws is followed by exception class names. https://javagoal.com/throw-and-throws/

+2

With throw you can raise an exception. Throws can be added to method signatures, to indicate which kind of Exceptions have to be expected when using it. You would do that e.g. when you create a file reader method with general approach, so that you cannot or do not want to handle the follow up workflow, but let the caller decide how to act on exceptions occuring during file access.