What is the point of the finally block?

后端 未结 16 1235
长情又很酷
长情又很酷 2020-12-30 20:19

Syntax aside, what is the difference between

try {
}
catch() {
}
finally {
    x = 3;
}

and

try {
}
catch() {
}

x = 3;
         


        
16条回答
  •  余生分开走
    2020-12-30 21:17

    In Java, you use it for anything that you want to execute regardless of whether you used a "return", just ran through the try block, or had an exception caught.

    For example, closing a database session or a JMS connection, or deallocating some OS resource.

    I am guessing it is similar in .NET?

提交回复
热议问题