Try catch and finally, Java block is used to handle an exception in programs. Each block has its own functionalities and is important. This block helps in preventing ugly application crashes and makes the application robust.
There is always a chance application code that may throw exceptions in runtime and you have to handle the exception by executing alternate application logic to report back to the user.
About try catch finally Java Blocks:-
All bocks are written with a keyword followed by the curly braces.
- try block – It contains the application code like reading a file, writing to databases, or performing complex business operations.
- catch block – It handles the checked exceptions thrown by try block as well as any possible unchecked exceptions.
- finally block – It an optional and typically used for closing files, network connections, etc.
The flow of try-catch-finally java blocks
If there are no exceptions then the catch block will not call and finally, the code will execute. Another condition if an exception will occur, then all blocks will be called.
On Exception or not, the next code line is working fine.
Exception Handling Examples
Simple example code.
Case 1: Without Exception
public class FinallyBlock { public static void main(String args[]){ try{ float data=95/9f; System.out.println(data); } catch(NullPointerException e){ System.out.println(e); } finally { System.out.println("finally block is always executed"); } } }
Output:
10.555555
finally block is always executed
Case 2: If the Exception occurs
public class FinallyBlock { public static void main(String args[]) { try { int data = 5 / 0; System.out.println(data); } catch (ArithmeticException e) { System.out.println(e); } finally { System.out.println("finally block is always executed"); } } }
Output:
java.lang.ArithmeticException: / by zero
finally block is always executed
Q: How to Java try-finally without a catch block?
Answer: You can use java to try and finally block without a catch. But you have to handle the error or depends on whether you can deal with the exceptions that can be raised at this point or not.
The finally
the block is typically used for closing files, network connections, etc.
See below example of it:- With the exception.
public class FinallyBlock { public static void main(String args[]){ try{ int data=9/0; System.out.println(data); } finally { System.out.println("finally block is always executed"); } } }
Output:
Q: How to handle exceptions in finally block java?
Answer: Is there an elegant way to handle exceptions that are thrown in finally block?
Usually, do it like this:
try { // Use the resource. } catch( Exception ex ) { // Problem with the resource. } finally { // Put away the resource. closeQuietly( resource ); }
Elsewhere:
protected void closeQuietly( Resource resource ) { try { if (resource != null) { resource.close(); } } catch( Exception ex ) { log( "Exception during Resource.close()", ex ); } }
Source: https://stackoverflow.com/questions/481446/throws-exception-in-finally-blocks
Do comment if you have any doubts and suggestions on this tutorial.
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java try, catch & finally blocks Example codes are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.