INTERVIEW QUESTIONS
J2EE
CORE JAVA
DETAILS
Question: What is the use of finally block?
Answer: The finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not. This is right place to close files, release your network sockets, connections, and perform any other cleanup your code requires.
Note: If the try block executes with no exceptions, the finally block is executed immediately after the try block completes. It there was an exception thrown, the finally block executes immediately after the proper catch block completes
|
Question:
What is the use of finally block?
Answer:
The finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not. This is right place to close files, release your network sockets, connections, and perform any other cleanup your code requires.
Note: If the try block executes with no exceptions, the finally block is executed immediately after the try block completes. It there was an exception thrown, the finally block executes immediately after the proper catch block completes Source: CoolInterview.com
Finally block is used to handle an exception(The common error which stop the complete execution of program.) in JAVA. It can be better understood by the following code:
try{ ......... code } catch(Exception e){ ........ code } finally{ ....... code }
Here finally will execute after catch block if there is any exception. And even there is no exception it will run.
It means the finally block always runs. Source: CoolInterview.com
Answered by: Jitender Kumar | Date: 11/18/2009
| Contact Jitender Kumar
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|