Exception Handling Practice from Many Exception Functions

If I have multiple functions in a function that throw exceptions, what is the best way to handle them if they depend on each other?

Depending on each other, what I mean is that if something throws exceptions, the code after the function that throws the exception should be skipped.

I figured out three ways to do this:

Nesting Exceptions

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!");
    }
}

      

Reverting to an 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 it all together and it's a mess. Is there any good practice on how to handle situations like this?

+3


source to share


2 answers


The way to use it depends on whether the exceptions should be handled by the client of the method, someFunction()

or whether they should be caught and handled as soon as they occur, that is, inside the method someFunction()

.

In case of attachment, no investment is required.
You can use one statement try

and place two calls that can throw exceptions in it.
If an exception occurs in one of the two called methods, you end up in one of the statements catch

, and therefore the second method is only executed if the first did not throw the caught exception. It produces exactly the same output as your code, but with one try

and no nesting, which 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 method is suitable if you have other statements after statements catch

and you want them to be executed even if one of the expected exceptions was caught.

The return on exception result reveals a fairly similar problem.
It can be refactored with one 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 method is suitable if you have several other instructions after the statements catch

, and you do not want to be executed if one of the expected exceptions was encountered.

However, in these two cases, if the exception handling is the same for the two exceptions ( Func1Exception

and Func2xception

), you can group them into a single operator catch:

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

      

Finally, adding exceptions to the method signature case only makes sense if the exception is handled by the method's client.

+3


source


You can catch multiple exceptions in a single catch clause since Java 7. I suppose.



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

      

+1


source







All Articles