+ 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...
6 Answers
+ 12
tq frmdz
+ 13
tq frmdz.....it helps a lot
+ 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.
+ 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.
+ 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
+ 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.