简体   繁体   中英

Determining which method throws exception

Below code catches an IOException , the first exception throw will be the one that is caught. To determine which method is throwing the IOException is the sole solution to wrap each method that throws an IOException in a try catch block ? I ask as my planned solution adds alot of try catch code and perhaps there is a cleaner solution to determine which method is throwing IOException ?

import java.io.IOException;
import java.net.SocketException;

public class Driver {

    private static void te() throws IOException {
        throw new java.net.SocketException("Connection Reset");
    }

    private static void te2() throws IOException {
        throw new java.net.SocketException("Connection Reset 2");
    }

    private static void te3() throws IOException {
        throw new java.net.SocketException("Connection Reset 3");
    }

    public static void main(String args[])  {

        try {
            Driver.te();
            Driver.te2();
            Driver.te3();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

The answer depends on your required logic. If the treatment of your exception is supposed to be different depending on which method threw the exception, (meaning that within catch you will need to write different error handling code depending on which method threw the exception, then you do need to wrap each method invocation into separate try-catch. But if the error handling is the same then your code is fine (except that usually, you print your stacktrace into a log file) and you would be able to figure out which method threw the exception by reading your stacktrace as a human user. But then again if the error handling is the same then your code doesn't need to know which specific method threw the exception.

I don't know why you are doing something like that, and surely a real situation would be far away from this code fragment but, in "real life" I would consider extending IOException : so you will have a single try with three catches in the main method. Do you like this solution?

Create a custom exception for each method:

class TeException extends IOException { /* constructor */ }

private static void te() throws TeException {
    throw new java.net.SocketException("Connection Reset");
}

Then it is fairly easy to distinguish among multiple exception with separate catch blocks:

 try {
    Driver.te();
    Driver.te2();
    Driver.te3();
} catch (TeException e) {
    e.printStackTrace();
} catch (Te2Exception e) {
    e.printStackTrace();
} catch (Te3Exception e) {
    e.printStackTrace();
}

An alternative might be to read the method that failed with the stacktrace:

final String failedMethodName = e.getStackTrace()[0].getMethodName());

You can do it as follows:

try {
    Main.te();
    Main.te2();
    Main.te3();
} catch (Exception e) {
    System.out.println(e.getStackTrace()[0].getMethodName());
}

In a real life scenario, you would probably be writing something to a log file in the case of an exception. For example:

public class Driver {
    // assuming slf4j
    private static Logger logger = LoggerFactory.getLogger(Driver.class);

    private static void te() throws IOException {
        logger.error("exception happened in te()");
        throw new java.net.SocketException("Connection Reset");
    }
}

Then, to figure out which methods threw exceptions, you would only need to open the log file and check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM