Difference Between throw and throws in Java — Java Technical Interview Questions & Answers
Difference Between throw and throws in Java
Difference Between throw and throws in Java
- throw is a statement inside the body: throw new IOException("missing");. throws is part of the signature: void read() throws IOException.
- throw sends one object. throws can list many types. throw actually happens. throws is only a declaration for checked exceptions.
- Answer with two columns, then one tiny method that uses both.
- Let's take this on the board with one tiny Main class — no extra files. Output: Asha scored 72 then Pass. Real Main. Swap marks/name to show one idea from Difference Between throw and throws in Java. Start from main, go line by line, and stop at each print. That output is the proof for Difference Between throw and throws.
Technical Interview Tip: Two columns: throw vs throws. Then write one method that uses both.
Code Examples for Difference Between throw and throws in Java
Example
// Difference
public class Main {
public static void main(String[] args) {
int marks = 72;
String name = "Asha";
System.out.println(name + " scored " + marks);
if (marks >= 40) System.out.println("Pass");
}
}Output: Asha scored 72 then Pass. Real Main. Swap marks/name to show one idea from Difference Between throw and throws in Java.
How to Explain Difference Between throw and throws in Java to an Interviewer
What is throw?
Statement Throws one object now
What is throws?
Signature warning Can list many types