Search code examples
javaignite

Java/Ignite - Function of Try Block


I'm new to Java, so this may be obvious, but consider the following two blocks of code. Version A leaves the server spinning forever, but Version B spins up then terminates the server. What is the function of the try block here?

Version A:

import org.apache.ignite.Ignition;
import org.apache.ignite.Ignite;
public class Test {
    public static void main(String[] args)
    {
        Ignite ignite = Ignition.start();
        return;
    }
}

Version B:

import org.apache.ignite.Ignition;
import org.apache.ignite.Ignite;
public class Test {
    public static void main(String[] args)
    {
        try(Ignite ignite = Ignition.start())
        {
            return;
        }
    }
}

Solution

  • Ignite instance is declared in a try-with-resource statement, it will be closed regardless of whether the try statement completes normally. For more details about this you can visit https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html