Dharmendra Singh
Why we need Finally key while we have catch?
By Dharmendra Singh in ASP.NET on Oct 09 2013
  • Dharmendra Singh
    Oct, 2013 28

    Aman Jain this is not good explanation, because if you are using catch the exception already handled and want to close connection here. so why you using finally.

    • 0
  • Aman Jain
    Oct, 2013 23

    We use the finally block when we have something that has to be executed anyhow, whether the code in try works fine or gives exceptions and goes to catch. Take an example when you have a Sql query and you are doing some sql operation in try code, you will obviously need to close that connection after the work is completed. And suppose you got an exception somewhere and it goes in catch block, then also the connection needs to be closed. So, in such cases where we have to do something irrespective of which block is executed, (like closing the connection at the end), we use finally block and close the connection in this. Whether the execution is of TRY or CATCH, FINALLY will be executed after them.Hope you understood. :)

    • 0
  • Dharmendra Singh
    Oct, 2013 15

    This not good answer. Priti

    • 0
  • Priti Kumari
    Oct, 2013 14

    If you want to some operation after the successfully code execution of Try block.write code in finally block. like close connection etc.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS