+ 5

What is the role of finally{} in exception handling of JAVA

18th Nov 2020, 11:31 AM
VAMSI
VAMSI - avatar
3 Answers
+ 2
https://www.sololearn.com/post/773494/?ref=app ROLE OF FINALLY IN JAVA The statements which are written in the finally block will execute 1)when exception is handled properly Also 2)when exception is not handled properly When exception handled properly the statements which are written next to the finally block will be executed When exception not handled properly the statements which are written next to the finally block will not be executed
21st Nov 2020, 7:32 AM
VAMSI
VAMSI - avatar
+ 3
Thank you pNK
18th Nov 2020, 11:52 AM
VAMSI
VAMSI - avatar
+ 2
The finally block is mainly used to close recourses for example when you make a Scanner object and you don't close it using the close method it could lead to memory leaks and that can be really bad when making big applications. You might be wondering doesn't execution just go to the next line after the catch block? Well yes it does but the key think about finally is that it always runs no matter what and this is useful especially if you were to change the program execution flow, that's the main advantage of finally is that it ignores you changing program execution flow and still runs whilst if you closed you recourses after the catch block without a finally block and the execution of your code changed, it'll still lead to memory leaks. Hope this helps
18th Nov 2020, 11:40 AM
pNK
pNK - avatar