When should I use the finally-block in Java's try-catch-finally

前端 未结 5 1087
北恋
北恋 2021-02-13 11:39

When should I use code snippet A instead of snippet B (i.e. what are the benefits of using snippet A)?:

Snippet A:

try {
    // codebloc         


        
5条回答
  •  不思量自难忘°
    2021-02-13 11:59

    You must almost always use the snippet with finally block when you have resources that needs clean up in both successful or error scenarios. A typical example is the jdbc connection object which should always be closed (clean up) in the finally block.

提交回复
热议问题