difference between throw, throws, and Throwable
Here's a clear and simple difference between throw
, throws
, and Throwable
in Java, with explanations and examples:
๐ Comparison Table
Keyword | Description | Use Case | Example Code |
---|---|---|---|
throw |
Used to manually throw an exception object | Inside method body | throw new ArithmeticException(); |
throws |
Declares that a method may throw exceptions (checked/unchecked) | In method signature | public void read() throws IOException |
Throwable |
Superclass of all exceptions and errors | For catching or creating custom exceptions | Throwable t = new Exception(); |
๐น 1. throw
– used to actually throw an exception
public class Example {
public static void main(String[] args) {
throw new ArithmeticException("Divide by zero");
}
}
- ๐ก Used inside methods
- Only one exception object can be thrown at a time
๐น 2. throws
– used to declare potential exceptions
public void readFile() throws IOException {
FileReader fr = new FileReader("abc.txt"); // may cause checked exception
}
- ๐ก Used in method declaration
- Can declare multiple exceptions
๐น 3. Throwable
– parent class of all exceptions and errors
public class Test {
public static void main(String[] args) {
try {
throw new Exception("Test");
} catch (Throwable t) { // catches Exception and Error
System.out.println("Caught: " + t);
}
}
}
- ๐ก Can catch any throwable (exceptions or errors)
- Rarely used directly, but helpful in logging/final catch block
๐ง Summary:
Term | Meaning | Type |
---|---|---|
throw |
Actually throws exception | Statement |
throws |
Declares possible exceptions | Keyword |
Throwable |
Base class of all errors & exceptions | Class |
Here are 7 commonly asked interview questions on throw, throws, and Throwable with concise answers:
---
✅ 1. What is the difference between throw and throws in Java?
Answer:
throw is used within a method to explicitly throw an exception.
throws is used in the method declaration to declare which exceptions can be thrown.
> Example:
throw new ArithmeticException();
public void read() throws IOException {}
---
✅ 2. Can we throw multiple exceptions using a single throw statement?
Answer:
No. throw can only throw one exception at a time.
throw new IOException(); // ✅ Only one exception
✅ 3. Can a method have multiple exceptions in the throws clause?
Answer:
Yes. Multiple exceptions can be declared using commas.
public void process() throws IOException, SQLException { }
---
✅ 4. Is Throwable a class or an interface? What is its role?
Answer:
Throwable is a class and the superclass of Exception and Error.
It is the root of Java's exception hierarchy.
---
✅ 5. Can we catch Throwable in a catch block?
Answer:
Yes. Catching Throwable lets you catch all exceptions and errors, but it's not recommended because it may also catch fatal errors (OutOfMemoryError, etc.).
catch (Throwable t) {
t.printStackTrace();
}
---
✅ 6. What happens if we declare an exception using throws but don't actually throw it?
Answer:
The code will compile and run fine, but it’s unnecessary.
Declaring without throwing has no effect.
---
✅ 7. Can we use throws for unchecked exceptions?
Answer:
Yes, but it's optional. Unchecked exceptions (RuntimeException and its subclasses) do not require throws declaration.
---
Comments
Post a Comment