By using try-catch-finally, how to ensure that there will be no another error in finally block? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

By using try-catch-finally, how to ensure that there will be no another error in finally block?

And what will be happen if there is error in finally block?

19th Jul 2016, 10:33 AM
WPimpong
WPimpong - avatar
4 Answers
+ 2
another try and catch... finally is usually used to close / dispose unneeded data, if you get exception in there you might want to close the program...
19th Jul 2016, 10:39 AM
Itay Almog
Itay Almog - avatar
+ 2
all errors should be caught, finally blocks are not supposed to do error handling, a work around would be to use a generic catch(Exception) which will ensure that any exceptions that fall through your catch statements are caught, a good use of the finally block would be to release a resource such as a db connection
21st Jul 2016, 5:43 AM
Eric Gitangu
Eric Gitangu - avatar
+ 2
All possible errors must be found out beforehand and the proper code for handling these errors should be written in the Catch block. A generic Exception for any other unforeseen errors must be the concluding code of the Catch block. Finally block is used to execute statements irrespective of the errors happening or not; so here resource release and garbage collection must be written. An error in the finally block is rare except if specifically written by the programmer. This amounts to Bad Coding and should be avoided at all costs.
7th Sep 2016, 7:44 AM
Naveen Karthik
Naveen Karthik - avatar
+ 1
You can make another try and catch inside finally block
19th Jul 2016, 11:06 AM
Mostafa Abobakr
Mostafa Abobakr - avatar