Java interview questions on throw
and throws
1️⃣ Can we use throw
and throws
together in the same method?
✅ Answer: Yes, we can use both throw
and throws
in the same method.
🔹 throws
is used in the method signature to indicate that the method may throw an exception.
🔹 throw
is used inside the method to actually throw an exception.
✅ Example:
💡 Trick: Many candidates confuse throw
and throws
, but remember that throw
actually throws the exception, while throws
just declares it.
2️⃣ Can a method declare throws
but never actually throw an exception?
✅ Answer: Yes, a method can declare throws
even if it does not explicitly throw an exception inside.
✅ Example:
💡 Trick: Some interviewers use this question to check if you understand that throws
is just a declaration and does not enforce throwing an exception inside the method.
3️⃣ What happens if we write throw
without specifying an exception?
✅ Answer: The code will not compile.
🔹 The throw
statement must be followed by an exception object.
🔹 Java requires an explicit exception object (of type Throwable
or its subclasses).
❌ Incorrect Code:
✅ Correct Code:
💡 Trick: Java does not allow a bare throw
statement without an exception object.
4️⃣ Can we throw multiple exceptions using throw
?
✅ Answer: No, throw
can only throw one exception at a time.
🔹 If multiple exceptions need to be thrown, they must be handled in separate conditions or wrapped in a combined exception.
❌ Incorrect Code:
✅ Correct Code:
💡 Trick: Unlike throws
, which can declare multiple exceptions, throw
can only send one exception at a time.
5️⃣ What happens if we declare a throws
clause in main()
?
✅ Answer: The main()
method can declare throws
, and any unhandled exception will be propagated to the JVM.
✅ Example:
💡 Trick: If main()
has throws Exception
, the program may crash unless handled properly using try-catch
.
6️⃣ Why should we prefer custom exceptions over generic exceptions in throw
?
✅ Answer: Custom exceptions provide better clarity and help in debugging issues more effectively.
🔹 Instead of throwing a generic exception like Exception
or RuntimeException
, we should create our own meaningful exceptions.
✅ Example:
💡 Trick: A well-defined exception name (like InsufficientBalanceException
) makes debugging much easier than throw new RuntimeException("Something went wrong")
.
7️⃣ What happens if we throw an exception inside a finally
block?
✅ Answer: If an exception is thrown inside the finally
block, it overrides any previous exceptions thrown in try
or catch
.
✅ Example:
Output:
💡 Trick: Any exception in finally
will suppress exceptions from try
and catch
, so avoid throwing exceptions from finally
unless necessary.
8️⃣ What happens if a checked exception is thrown but not declared using throws
or handled using try-catch
?
✅ Answer: The code will not compile because Java enforces checked exceptions to be either:
-
Handled using
try-catch
-
Declared using
throws
❌ Incorrect Code (Compilation Error):
✅ Correct Code:
💡 Trick: Java forces you to handle checked exceptions, unlike unchecked exceptions.
9️⃣ Can we override a method and remove throws
from the overridden method?
✅ Answer: Yes, a subclass can remove the throws
clause while overriding a method, but it cannot declare new broader exceptions than the superclass.
✅ Example:
💡 Trick: A subclass can handle exceptions internally and remove throws
, but it cannot declare new broader exceptions (e.g., Exception
instead of IOException
).
🔟 Can we use throws
with finally
?
✅ Answer: Yes, but the finally
block executes before the method exits, even if an exception is thrown.
✅ Example:
💡 Trick: finally
will always execute before the exception propagates to the caller.
🔥 Quick Recap (for Interviews)
✅ throw
→ Actually throws an exception.
✅ throws
→ Declares that a method may throw an exception.
✅ throw
requires an exception object.
✅ throws
does not require an object—just a declaration.
✅ A method can throws
multiple exceptions but can only throw
one at a time.
✅ Exceptions in finally
override exceptions from try-catch
.
No comments:
Post a Comment