Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

Does throwing an exception stop execution java?

Asked by Amit Sinha Oct 10, 2022 8.3K views 3 answers
Share

About this question

There is a recommendation not to use an Exception to control the flow of execution. This gives me a reason for doubt when I do something like this:


 public void initApplication(String input) {
        try {
            MyClass mClass = new mClass(input);
            saveToDatabase(mClass);
            updateSettingsTheAppIsReady();
        } catch (CancelPreparationsException e) {
            showErrorMessageToUser();
            doRollbacks();
        }

    }

mClass instantiation, save operation to a database, settings update may go wrong, each of them throw their own exceptions, which I catch within the methods/classes and throw my custom exception to stop the initApplication() method execution.

This sounds an easier way than having preconditions checking like:

Create a MyClass object.

Check the MyClass object is created then save it to db.

Ensure the object was saved in db then update settings.

Is it really not a recommended way of using the Exception to control the flow? Thank you.

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Mar 5, 2025

Yes, throwing an exception in Java stops execution unless it is caught and handled. Here’s how it works:

1. What Happens When an Exception is Thrown?

  • When Java encounters a throw statement, it immediately stops executing the current method.
  • The exception propagates up the call stack, looking for a matching catch block.
  • If no catch block is found, the program terminates with an error message.

2. Example: Unhandled Exception (Stops Execution)

public class Test {
    public static void main(String[] args) {
        System.out.println("Before exception");
        throw new RuntimeException("Something went wrong!"); 
        // Code below will never execute
        // System.out.println("After exception");
    }
}

Was this helpful?

Ranjana Admin JanBask Expert

Answered on May 8, 2024

In Java, throwing an exception doesn't necessarily halt the execution of your program by default. When an exception is thrown, the Java runtime system looks for an appropriate exception handler to catch and handle the exception.


If there's a try-catch block surrounding the code that throws the exception, and if there's a catch block that matches the type of exception thrown, then the execution will transfer to that catch block. After the catch block executes, the program continues its execution unless there's another exception thrown.

If no appropriate catch block is found, the exception will propagate up the call stack until it's caught or until it reaches the top-level exception handler, at which point the program will typically terminate and print out the exception stack trace.

However, if you throw an exception within a finally block, it will override any exception that occurred in the try block, but it won't stop the execution of your program. The original exception will still be thrown after the finally block completes.

So, in short, throwing an exception doesn't always stop the execution of a Java program, but it can halt the normal flow depending on how the exception is handled in your code.

Was this helpful?

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.