What is the difference between throw and throws clause?
Long Answer
Views 60
Answer:
Solution:
| Keyword | Purpose | Used In | Example |
|---|---|---|---|
| throw | Used to explicitly throw an exception from a method or block of code. | Inside a method or block. | throw new IOException("File not found"); |
| throws | Used to declare that a method may throw certain exceptions. | In the method declaration. | void readFile() throws IOException |
Explanation:
-
throwis used when you want to manually raise an exception. -
throwsis used when you want to inform the caller that this method might throw an exception that it must handle.
Related Articles:
This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.