Java Technology

Java Technology

Share

06/03/2026

package java8;

import java.util.Arrays;
import java.util.Comparator;

public class ArraysOfString {

public static void main(String[] args) {
String[] arr = {"aa", "a", "bb", "ccc"};
Arrays.stream(arr)
.sorted(Comparator.comparing(String::length))
.forEach(System.out::println);

}
}

o/p : a aa bb ccc

07/09/2025

try-catch-finally with return in Java

Case 1: return in try, but finally executes

public class Test {
public static int testMethod() {
try {
System.out.println("Inside try");
return 1;
} catch (Exception e) {
System.out.println("Inside catch");
return 2;
} finally {
System.out.println("Inside finally");
}
}

public static void main(String[] args) {
System.out.println("Result = " + testMethod());
}
}
//output
Inside try
Inside finally
Result = 1

Case 2: return in finally (overrides try/catch)

public class Test {
public static int testMethod() {
try {
System.out.println("Inside try");
return 1;
} catch (Exception e) {
System.out.println("Inside catch");
return 2;
} finally {
System.out.println("Inside finally");
return 3;
}
}

public static void main(String[] args) {
System.out.println("Result = " + testMethod());
}
}
//output
Inside try
Inside finally
Result = 3

Note : If you return inside finally, it overrides any previous return.

Case 3: Exception occurs in try

public class Test {
public static int testMethod() {
try {
int x = 10 / 0; // Exception
return 1;
} catch (Exception e) {
System.out.println("Inside catch");
return 2;
} finally {
System.out.println("Inside finally");
}
}

public static void main(String[] args) {
System.out.println("Result = " + testMethod());
}
}

//Output:
Inside catch
Inside finally
Result = 2

Catch handled the exception, finally executed, then return value from catch.

Golden Rules to remember (interview points):
1. finally always executes (except System.exit or JVM crash).
2. If finally has a return statement, it overrides try/catch returns.
3. Avoid return inside finally → makes debugging very confusing.

Case 4: Using System.exit() inside try

public class Test {
public static int testMethod() {
try {
System.out.println("Inside try");
System.exit(0); // Terminates JVM immediately
return 1;
} catch (Exception e) {
System.out.println("Inside catch");
return 2;
} finally {
System.out.println("Inside finally");
return 3;
}
}

public static void main(String[] args) {
System.out.println("Result = " + testMethod());
}
}
//output
Inside try
After printing "Inside try", the JVM shuts down.
• finally is NOT executed.
• The return statement is never reached.

Case 5: Using System.exit() inside catch
public class Test {
public static int testMethod() {
try {
int x = 10 / 0; // ArithmeticException
return 1;
} catch (Exception e) {
System.out.println("Inside catch");
System.exit(0); // JVM shuts down here
return 2;
} finally {
System.out.println("Inside finally");
return 3;
}
}

public static void main(String[] args) {
System.out.println("Result = " + testMethod());
}
}

//output
Inside catch

Again, finally is skipped because JVM exits immediately.

Key Interview Notes:
• Normally, finally always executes.
• But two exceptions:
1. If JVM terminates abnormally (e.g., crash, kill -9).
2. If you explicitly call System.exit() → JVM shuts down, so finally is skipped.

All important points about try-catch-finally with return in Java
• If return is inside try, the expression is evaluated, but finally always executes before returning.
• If return is inside catch, same rule — finally executes before returning.
• If finally contains no return, then the value from try/catch is returned after finally finishes.
• If finally itself has a return, that overrides and discards any previous return from try/catch.
• If finally modifies a variable (but does not return), the evaluated return value from try/catch remains unchanged.
• If System.exit(0) (or JVM crash) happens in try/catch, then finally does not execute.
• So rule of thumb: finally block always executes (except JVM termination), and its return (if present) takes highest priority.

Want your school to be the top-listed School/college in Delhi?
Click here to claim your Sponsored Listing.

Category

Telephone

Address


G-6/365 FF Sec-16 Rohini New Delhi
Delhi
110085