Hai ol....can anyone say clear usage of try and final blocks?!! Plzz | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15

Hai ol....can anyone say clear usage of try and final blocks?!! Plzz

to get clear usage of try and final blocks in program...

8th Mar 2017, 3:29 AM
SHOBHANA.G
SHOBHANA.G - avatar
6 Answers
+ 12
tq frmdz
8th Mar 2017, 4:04 AM
SHOBHANA.G
SHOBHANA.G - avatar
+ 13
tq frmdz.....it helps a lot
11th Mar 2017, 7:26 AM
SHOBHANA.G
SHOBHANA.G - avatar
+ 12
there are two keyword final and finally. both are used for different purpose. final : it is used to make constant variable, non-inheritable class, and non-override method. finally : it used in exception handling, with try-catch. it is used to execute statement in every condition, even if program goes to be crashed.
11th Mar 2017, 7:25 AM
Ajay Agrawal
Ajay Agrawal - avatar
+ 5
we put few piece of code(whose execution may get abnormally terminate) in try/catch/finally for the smooth execution of program. Exception handling will handle it. Final is a keyword not block used for creating constant when used with literals, restricts overriding of methods when used with methods, restricts inheritance of class when used with class. Finally is a block with a piece of code executes always irrespective of exception in try or catch. only in two condition piece of code within finally won't run, either when you call System.exist(0) before block or due to some jvm error.
10th Apr 2017, 3:49 PM
Tarique Khan
Tarique Khan - avatar
+ 3
do you mean try catch? the way try catch works is you place your code inside the try block. If an exception is thrown by your code, either because you meant to create the exception or because the computer created one due to an unforseen error, then the catch block handles the exception. It's a good way to handle parts of code and prevent the program from crashing. If someone gives you a bad value on an input you can let them know and not just kill the program. Hope this helps
8th Mar 2017, 3:39 AM
Richard Valles
Richard Valles - avatar
+ 3
Adding on to what Richard Vallas said, the finally block can be used to handle anything that needs to be executed even if an exception was thrown or not. Assume you successfully open a connection to a DB and read data from a table and during the read and exception was thrown that takes you back to the start of opening a new connection, you could place code in the finally block to close the previous connection to the DB, because closing the connection needs to happen regardless of an exception or no exception.
8th Mar 2017, 3:58 AM
Elfren Authorlee
Elfren Authorlee - avatar