What is the difference between throw and throws keywords?

Technical
Medium
TCS
59.9K views

A specific syntax question testing detailed knowledge of exception propagation mechanisms in languages like Java.

Why Interviewers Ask This

It verifies precise understanding of how exceptions are raised versus declared. Misunderstanding this leads to compilation errors or poor error flow design.

How to Answer This Question

Explain that 'throw' is used to explicitly raise an exception inside a method. 'Throws' is used in the method signature to declare which exceptions might be thrown. Clarify that 'throws' informs the caller to handle them.

Key Points to Cover

  • Explicit raising
  • Method declaration
  • Propagation rules
  • Syntax placement

Sample Answer

The 'throw' keyword is used to manually trigger an exception within a method body when a specific error condition is met. The 'throws' keyword appears in the method declaration to list exceptions that the method might propagate. 'Throw' raises the error, while 'throws' declares responsibility.

Common Mistakes to Avoid

  • Using them interchangeably
  • Confusing scope
  • Misplacing keywords

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 78 Technical questionsBrowse all 79 TCS questions