The finally Block in Java


Use finally to execute code after try and catch blocks, regardless of whether an exception was thrown or caught, often used for cleanup code like closing file streams or database connections.

Source Code

try {
    int[] myNumbers = {1, 2, 3};
    System.out.println(myNumbers[10]);
} catch (IndexOutOfBoundsException e) {
    System.out.println("Index is out of bounds!");
} finally {
    System.out.println("The try-catch is finished.");
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments