Exception handling | SoloLearn: Learn to code for FREE!

+1

Exception handling

Why is it that if a try block doesn't have any exception then my try block is executed first and then my finally block. But if I have an exception in my try block then my finally block executes first and then the exception message is shown.

11/21/2019 10:45:58 AM

Avinesh

2 Answers

New Answer

+4

That is because exception transfers the flow of code to the place where it is handled (never to return again) or not, so finally block needs to be executed to free up resources or whatever you want to do before the transfer of code flow. When there is no exception the statements are simply executed normally.

+1

~ swim ~ Thank you, got it.