Finally Vs Finalize
Finally : The finally block comes with try-catch block. Whatever is inside in the finally block it will execute whether the try-catch block will execute or not. This ensures that the finally block is executed even if an unexpected exception occurs
So why we need finally block?? Suppose you are trying to access one file inside the try block. What we do if we want to access the file, we open the file. And now some exception occurs while accessing the file the flow of the code will go to the catch block to handle the exception, and then the code outside the try-catch block will execute. So what happened?? First we open some file to access and then due to some exception the flow goes outside of the try-catch block and program continues .. But java forget to close the file which was open inside the Try block. This means Java still hold the reference of that file inside the memory . So whether the code execute or not we need to close the file after finishing its access. Hence we need finally block to ensure resources are recovered regardless of any problems that may occur.