Can the main()
Method Throw an Exception in Java?
✅ Yes, the main()
method can throw an exception outside using the throws
keyword.
🚨 However, if an exception is not handled inside main()
, it will be handled by the JVM (Java Virtual Machine), which will print a stack trace and terminate the program.
1. Syntax: Declaring throws
in main()
public static void main(String[] args) throws Exception {
// Code that may throw an exception
}
- The
throws
keyword declares thatmain()
may throw an exception. - It does not handle the exception—it simply allows it to propagate.
2. Example: main()
Throwing an Exception Without Handling
public class MainThrowsExample {
public static void main(String[] args) throws Exception {
throw new Exception("Unhandled exception in main");
}
}
🛑 Output (JVM Handles It):
Exception in thread "main" java.lang.Exception: Unhandled exception in main
at MainThrowsExample.main(MainThrowsExample.java:3)
🔹 Key Takeaway: The exception is not handled inside main()
, so JVM catches it and prints a stack trace.
3. Handling Exceptions Inside main()
To prevent the program from crashing, you should handle exceptions using try-catch
inside main()
.
✅ Example: Handling Exception Inside main()
public class MainExceptionHandling {
public static void main(String[] args) {
try {
throw new Exception("Handled exception in main");
} catch (Exception e) {
System.out.println("Caught exception: " + e.getMessage());
}
}
}
✅ Output:
Caught exception: Handled exception in main
🔹 Key Takeaway: Handling exceptions inside main()
prevents JVM from terminating the program.
4. main()
Throwing Exceptions from Other Methods
If main()
calls another method that throws an exception, it must handle or propagate it.
✅ Example: Propagating Exception from Another Method
public class ExceptionPropagation {
public static void riskyMethod() throws Exception {
throw new Exception("Exception from riskyMethod");
}
public static void main(String[] args) throws Exception { // Propagating exception
riskyMethod();
}
}
🛑 Output (JVM Handles It, Program Crashes):
Exception in thread "main" java.lang.Exception: Exception from riskyMethod
at ExceptionPropagation.riskyMethod(ExceptionPropagation.java:3)
at ExceptionPropagation.main(ExceptionPropagation.java:7)
🔹 Key Takeaway: main()
does not catch the exception, so JVM handles it and prints a stack trace.
✅ Example: Handling Exception in main()
public class HandleExceptionInMain {
public static void riskyMethod() throws Exception {
throw new Exception("Exception from riskyMethod");
}
public static void main(String[] args) {
try {
riskyMethod();
} catch (Exception e) {
System.out.println("Exception handled in main: " + e.getMessage());
}
}
}
✅ Output:
Exception handled in main: Exception from riskyMethod
🔹 Key Takeaway: Handling the exception prevents JVM from terminating the program.
5. What Happens If an Unchecked Exception Is Thrown in main()
?
Since unchecked exceptions (RuntimeException
) do not require handling, they can also be thrown in main()
without throws
.
✅ Example: Unhandled RuntimeException
in main()
public class UncheckedExceptionExample {
public static void main(String[] args) {
throw new RuntimeException("Unhandled runtime exception in main");
}
}
🛑 Output (JVM Handles It, Program Crashes):
Exception in thread "main" java.lang.RuntimeException: Unhandled runtime exception in main
at UncheckedExceptionExample.main(UncheckedExceptionExample.java:3)
🔹 Key Takeaway: JVM catches unhandled unchecked exceptions just like checked exceptions.
✅ Example: Handling RuntimeException
Inside main()
public class HandleUncheckedException {
public static void main(String[] args) {
try {
throw new RuntimeException("Runtime exception handled in main");
} catch (RuntimeException e) {
System.out.println("Caught runtime exception: " + e.getMessage());
}
}
}
✅ Output:
Caught runtime exception: Runtime exception handled in main
🔹 Key Takeaway: Handling unchecked exceptions prevents program termination.
6. Summary
Scenario | Does JVM Handle It? | Recommended Approach |
---|---|---|
Checked exception (Exception ) thrown in main() but not caught | ✅ Yes (program crashes) | ❌ Bad practice |
Checked exception thrown in main() and caught | ❌ No (program continues) | ✅ Good practice |
Checked exception thrown from another method, propagated by throws in main() | ✅ Yes (program crashes) | ❌ Bad practice |
Unchecked exception (RuntimeException ) thrown in main() but not caught | ✅ Yes (program crashes) | ❌ Bad practice |
Unchecked exception handled inside main() | ❌ No (program continues) | ✅ Good practice |
Final Thoughts
✔ Yes, main()
can throw exceptions, but if unhandled, JVM will catch them and terminate the program.
✔ It’s always recommended to handle exceptions inside main()
using try-catch
.
✔ Unchecked exceptions (RuntimeException
) behave the same way—JVM catches them if not handled.
✔ If calling methods that throw checked exceptions, main()
must either catch them or declare throws
.