1

I have a project having Exception handling written in the following way:

Parent class has all the exception handling logic. And the invoked class just throws exception and the invoker class handles with appropriate logic.

Now the problem that I am facing invoked class opens different stuffs for example, a file. These files are not getting closed at the time of exception.

so what should be the appropriate way of exception handling in this case.

    class A
    {
    private void createAdminClient()
    {

        try
        {
            B b = new B();          
                b.getClinetHandler();
        }
        catch(CustomException1 e1)
        {
        }   
        catch(CustomException2 e1)
        {
        }   
        catch(CustomException3 e1)
        {
        }
        catch(CustomException4 e1)
        {
        }
    }
}

class B
{
    ................
    ................

    getClinetHandler() throws Exception
    {
        --------------------------      
        ---- open a file----------
        --------------------------
        ----lines of code---------
        --------------------------      

        Exceptions can happen in these lines of code.
        And closing file may not be called      

        --------------------------      
        ---- close those files----
        --------------------------

    }

}

4 Answers 4

2

You can wrap the code which may throw an exception in a try...finally block:

getClientHandler() throws Exception {
    // Declare things which need to be closed here, setting them to null
    try {
        // Open things and do stuff which may throw exception
    } finally {
        // If the closable things aren't null close them
    }
}

This way the exception still bubbles up to the exception handler but the finally block ensures that the closing code still gets called in the event of an exception.

Sign up to request clarification or add additional context in comments.

Comments

0

use a finally block to complete the final tasks. for example

    try
    {
        B b = new B();          
            b.getClinetHandler();
    }
    catch(CustomException1 e1)
    {
    }  
    finally{
       // close files
    }

From the doc

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

Comments

0

This is how I do it

try {
    //What to try
} catch (Exception e){
        //Catch it
    } finally {
                    //Do finally after you catch exception
        try {
            writer.close(); //<--Close file
        } catch (Exception EX3) {}
    }

Comments

0

Use a finally block to handle post-processing execution (regardless whether it succeeded or failed). Like so:

// Note: as Sean pointed out, the b variable is not visible to the finally if it 
// is declared within the try block, therefore it will be set up before we enter
// the block.
    B b = null; 

    try {
        b = new B();          
        b.getClinetHandler();
    }
    catch(CustomException1 e1) {
    } // and other catch blocks as necessary...
    finally{
        if(b != null)
            b.closeFiles() // close files here
    }

The finally block is always executed, regardless, even if you throw or return from the try or catch blocks.

This answer provides a very good explanation of how the finally block works in this situation, and when/how it is executed, and basically further illustrates what I just wrote.

4 Comments

This isn't what they want. The closable objects aren't visible in this code block.
@Sean I think it is what they want, as they are ultimately asking how to close the files after execution (in the getClientHandler method). However you have indeed pointed out a flaw, in that the b variable is not visible, which I will correct.
Hi Teeg, the code snippet that I have pasted, basically the parent class has all the exception handling logic. If I choose this way in that case closeFiles and other handling closable objects handling logic has to go to the parent- which I do not want.
@Baishakh Ah ok, fair enough that makes sense. Sean's answer is a better design approach in that regard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.