Putting try catch finally block inside another finally block

前端 未结 5 1100
孤独总比滥情好
孤独总比滥情好 2021-02-07 00:57
 try {
 } catch() {}
 finally {
     try {
     } catch() { }
     finally { }
 }

Is it good to have the code like above?

5条回答
  •  名媛妹妹
    2021-02-07 01:34

    Yes, you can do this.

    Actually, you are even required to do it when dealing with streams you want to close properly:

    InputStream in = /* ... */;
    try {
    } catch (...) {
    } finally {
        try {
            in.close();
        } catch (...) {
        } finally {
        }
    }
    

    I don't see any case in which this would be a bad practice

提交回复
热议问题