Please tell me the business scenario for 'Finally block' in exception handling means a business scenario where we need finally block
Asked
Active
Viewed 22 times
1 Answers
0
Finally blocks are used for clean up that must happen whether the code inside the try block fails or not. A common use case would be a stream reader that reads text from a file. The file must be closed in either case of success or failure.
Also, the finally block has the unique feature of always being executed even if the try (or catch) block returns from the method, e.g
bool MyFunction()
{
try
{
return SomethingThatMightFail()
}
finally
{
DoSomethingBeforeReturningOrFailing()
}
}

Claus Nielsen
- 476
- 6
- 11