Example & Tutorial understanding programming in easy ways.

What happens when exception is thrown by main method?

When exception is thrown by main() method, Java Runtime terminates the program and print the exception message and stack trace in system console.

The throws clause only states that the method throws a checked FileNotFoundException and the calling method should catch or rethrow it. If a non-checked exception is thrown (and not catch) in the main method, it will also terminate.

Check this example:-

public class ExceptionThrownTest {

    public void testingExceptions() {

        try {
            ExceptionThrownTest.main(new String[] {});
        } catch (Throwable e) {
            assertTrue(e instanceof RuntimeException);


    public static void main(String[] args) throws FileNotFoundException {


        // it will note be executed because RuntimeException thrown


    private static void dangerousMethod() {
        throw new RuntimeException();

    private static void unreachableMethod() {
        System.out.println("Won't execute");

As you can see, if exception throw a RuntimeException the method will terminate even if the exception thrown is not a FileNotFoundException

Read More →
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!