Exception handling try catch inside catch

前端 未结 3 1872
别跟我提以往
别跟我提以往 2020-12-13 17:59

I recently came across code written by a fellow programmer in which he had a try-catch statement inside a catch!

Please forgive my inability to paste the actual code

3条回答
  •  旧巷少年郎
    2020-12-13 18:20

    Here is a case :

    try{
        //Dangerous Operation
    } catch (AnyException ae) {
        try {
            //Do rollback which can fail
        } catch (RollbackFailedException rfe) {
            //Log that
        }
    } finally {
        try {
            //close connection but it may fail too
        } catch (IOException ioe) {
            //Log that
        }
    }
    

    It's about the same thing as @x0n said. You might need to handle exception while try to close resources or while you're trying to resolve a situation brought by another exception.

提交回复
热议问题