Is it possible to execute a program without a catch block? If so, how would you use try and finally together?

Yes, it is possible to use a try block without a catch block in Java, provided it is followed by a finally block. In this case, any exception thrown in the try block is not handled locally and propagates to the caller, while the finally block still executes to perform cleanup operations.

Key Points: • A try block can be used with either catch, finally, or both. • When no catch block is present, exceptions are propagated to the calling method. • The finally block executes regardless of whether an exception occurs. • finally is commonly used for releasing resources such as files, database connections, or network sockets. • This approach is useful when exception handling is intended to be performed at a higher level.

Example: A method may open a file inside the try block and close it in the finally block, while allowing any exception to be handled by the caller.

Code Example:

public class Demo {

    public static void main(String[] args) {

        try {
            int result = 10 / 0;
        } finally {
            System.out.println("Cleanup code executed");
        }
    }
}

Output:

Cleanup code executed

Exception in thread "main" java.lang.ArithmeticException: / by zero

Interview Tip: A concise interview answer is:

"Yes, Java allows a try block without a catch block if it is followed by a finally block. In such cases, the exception is propagated to the caller, but the finally block still executes, making it useful for resource cleanup and releasing system resources."