Yes, it is possible to use a final block to try to execute without a catch block.
We know that the final block will always be executed, even if an exception occurs in the try block, unless System.exit() is used, it will always be executed.
public class TryBlockWithoutCatch { public static void main(String[] args) { try { System.out.println("Try Block"); } finally { System.out.println("Finally Block"); } } }
Try Block Finally Block
Even if the method has a return type and the try block returns some value, the final block will still execute.
public class TryWithFinally { public static int method() { try { System.out.println("Try Block with return type"); return 10; } finally { System.out.println("Finally Block always execute"); } } public static void main(String[] args) { System.out.println(method()); } }
Try Block with return type Finally Block always execute 10
The above is the detailed content of In Java, can we use try block without catch block?. For more information, please follow other related articles on the PHP Chinese website!