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

前端 未结 5 1092
北恋
北恋 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 12:07

    Use a finally block if you have code that must execute regardless of whether or not an exception is thrown.

    Cleaning up scarce resources like database connections are a good example.

提交回复
热议问题