What will happen if the exception is not handled using try catch block during an exception propagation(Checked exception)

What will happen if the exception is not handled using try catch block during an exception propagation(Checked exception)

What will happen if handling the exception check reaches the last stack in the stack unit during the exception propagation and the checked exception is not handled using try-catch.

I am calling a methodB using methodA from the main method and the methodB is having an checked exception and the methodB is using throws checkedException and forcing the methodA to handle the exception and similarly the methodA is also using the throws checkedException and forcing the main method to handle the exception. The main method is not using the try-catch to handle the exception rather it is also using throws checkedException. So, the exception propagation reaches the last stack in the stack unit. Here the main method is forcing JVM to handle the exception but the JVM calling the main() method is a runtime action. So here the checked exception is handled as the code is compiled but, in the runtime the exception is occurring. Is a checked exception theoretically converted into an unchecked exception if it's not handled during propagation?

Main Class code

public class MainApplication {
    public static void main(String []args) throws IOException {
        ClassA.myMethod();
    }
}

Class A(methodA) code

public class ClassA {
    public static void myMethod() throws IOException {
        ClassB.MyMethod();
    }
}

ClassB(methodB) code

public class ClassB {
    public static void MyMethod() throws IOException {
        throw new IOException();
    }
}

Error:

Exception in thread "main" java.io.IOException
    at Exceptions.ClassB.MyMethod(ClassB.java:10)
    at Exceptions.ClassA.myMethod(ClassA.java:6)
    at MainApplication.main(MainApplication.java:25)

Answer

Is a checked exception theoretically converted into an unchecked exception if it's not handled during propagation?

No. The exception is still checked. You declared main() as throws IOException, which is one of the acceptable check conditions, so the IOException is being checked at compile-time. You are simply not handling the exception in a try..catch block when it's thrown at run-time, you are letting the JVM handle it. The JVM will handle it by terminating your app.

Enjoyed this question?

Check out more content on our blog or follow us on social media.

Browse more questions