Dave-it
Dave-it

Reputation: 33

Exception handling practices of multiple exception throwing functions

If I have multiple functions in a function which are throwing exceptions what's the best way of handling them if they depend on each other?

With depending on each other I mean that if something throws an exceptions the code after the function which threw the exception should be skipped.

I figured out three ways to do this:

Exception nesting

public void parent() {
    someFunction();
}

public void someFunction() {
    try {
        function1();
        try {
            function2();
            ...
        } catch (Func2xception e) {
            System.out.println("Function 2 failed!");
        }
    } catch (Func1Exception e) {
        System.out.println("Function 1 failed!");
    }
}

Return on exception

public void parent() {
    someFunction();
}

public void someFunction() {
    try {
        function1();
    } catch (Func1Exception e) {
        System.out.println("Function 1 failed!");
        return;
    }

    try {
        function2();
    } catch (Func2xception e) {
        System.out.println("Function 2 failed!");
        return;
    }

    ...
}

Add exceptions to method signature

public void parent() {
    try {
        someFunction();
    } catch (Func1Exception e) {
        System.out.println("Function 1 failed!");
    } catch (Func2Exception e) {
        System.out.println("Function 2 failed!");
    } ...
}

public void someFunction() throws Func1Exception, Func2Exception {
    function1();
    function2();
    ...
}

I sometimes use all of them together and that's a mess. Are there any good practices on how to handle situations like this?

Upvotes: 3

Views: 1458

Answers (2)

davidxxx
davidxxx

Reputation: 131456

The way to use depends on whether the exceptions should be handled by the client of the someFunction() method or else caught and handled as soon as they happen, that is, inside the someFunction() method.

In the exception nesting case, the nesting is not required.
You can use a single try statement and place the two calls that may generate the exceptions in it.
If an exception occurs in one of the two invoked methods, you finish in one of the catch statements and so the second method is executed only if the first one has not thrown the caught exception. It produces exactly the same result than your code but with a single try and without nesting that is less readable.

public void someFunction() {
    try {
        function1();    
        function2();
            ...      
    } catch (Func1Exception e) {
        System.out.println("Function 1 failed!");
    }
    catch (Func2xception e) {
        System.out.println("Function 2 failed!");
    }    
}

This way of doing is suitable if you have some other instructions after the catch statements and you want them to be executed even if one of the expected exceptions was caught.

The return on exception case manifests a close enough problem.
It may be refactored with a single try :

    public void someFunction() {
        try {
            function1();    
            function2();
                ...      
        } catch (Func1Exception e) {
            System.out.println("Function 1 failed!");
            return;
        }
        catch (Func2xception e) {
            System.out.println("Function 2 failed!");
            return;
        }    
    }
    ...
}

This way of doing is suitable if you have some other instructions after the catch statements and you don't want them to be executed if one of the expected exceptions was caught.

Nevertheless for these two cases, if the exception handling is the same for the two exceptions (Func1Exception and Func2xception), you could group them in a single catch statement :

public void someFunction() {
    try {
        function1();    
        function2();
            ...      
    } 
    catch (Func1Exception | Func2xception e) {
        System.out.println("Function 1 or 2 failed!");
    }
}

At last, the add exceptions to method signature case makes sense only if the exceptions should be handled by the client of the method.

Upvotes: 3

Nayeem Zen
Nayeem Zen

Reputation: 2767

You can catch multiple exceptions in one catch clause, starting from Java 7 I believe.

try { 
  ...
} catch(IOException | IllegalArgumentException | SomeOtherException e) { 
  ...
}

Upvotes: 1

Related Questions