Java
programming
methods
control flow
return statement

How to exit a method in Java (return, exceptions and loops)

Codemia Knowledge Hub ·

Use the return statement. In a method with a return type it exits and hands a value back to the caller; in a void method a bare return; simply exits. A method also exits when it throws an exception or reaches the end of its body.

Exiting with return

java
1   public int calculateSum(int a, int b) {
2       return a + b; // Exits method and returns the sum
3   }
4
5   public void checkValue(int a) {
6       if (a < 0) {
7           System.out.println("Negative value encountered");
8           return; // Exits the method
9       }
10       System.out.println("Value is non-negative");
11   }

Exiting by throwing an exception

Throwing an exception exits the current method immediately and transfers control to a matching exception handler, if one exists. Use it to signal abnormal conditions.

java
1   public int divide(int numerator, int denominator) {
2       if (denominator == 0) {
3           throw new IllegalArgumentException("Denominator cannot be zero");
4       }
5       return numerator / denominator; // Normal exit if no exception thrown
6   }

Reaching the end of the method

A void method exits naturally after its last statement, with no explicit return needed:

java
1   public void greeting() {
2       System.out.println("Hello, World!");
3       // Implicitly exits after last statement
4   }

A method with a non-void return type must return a value on every path. Reaching the end of it without returning one is a compilation error.

Returning from inside a loop

A conditional return inside a loop leaves the whole method as soon as the condition is met, not just the loop:

java
1public void findNumber(int[] numbers, int target) {
2    for (int number : numbers) {
3        if (number == target) {
4            System.out.println("Target found: " + target);
5            return; // Exits the method
6        }
7    }
8    System.out.println("Target not found");
9}

Here the method exits the moment the target is found, so "Target not found" only prints when the loop finishes without a match.

TechniqueDescriptionUsage Example
return statementExits method naturally, optionally returning a valuereturn a + b;
Exception throwingExits method due to abnormal state, passing control to handlersthrow new IllegalArgumentException("message");
End of method blockImplicit method exit after last statement in void methodsSystem.out.println("Done"); // Exits implicitly
return in loopsExits method during loop execution if certain conditions are metif(condition) return; // Exits method

Why exit early

  1. Performance: Returning early skips unnecessary work, especially in loops.
  2. Readability: A well-placed exit makes it clear which conditions end the method.
  3. Error handling: Exceptions enforce strict error handling without cluttering your logic with conditional checks.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms