Throws Clause Example in Java
Introduction
Throws clause is a classic Java console program that demonstrates the concept with complete source code and sample output. Exceptions represent runtime errors; Java uses try-catch-finally to handle them safely.
This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.
Syntax
<return_type> <method_name>() throws <exception_name>{
//Do Something
}Enter number 1 :
100
Enter number 2 :
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at learnjavaprograms.ThrowsClause.divide(ThrowsClause.java:17)
at learnjavaprograms.ThrowsClause.main(ThrowsClause.java:26)
Java Result: 1Sample Output
Run the program locally to see the output.When to use
Use this throws clause example when learning or revising core Java syntax.
How it works
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
Compare your console output with the sample output for Throws clause to confirm the program behaves correctly.
Best Practices
Common Mistakes
Frequently Asked Questions
What does the Throws clause program demonstrate?
It shows how to implement throws clause in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.