Best practices for Java exception handling

I recently wrote the following code; It uses a lot of exception handling I think it makes the code look very difficult to understand I can shorten the code by catching generic exceptions, such as

catch (Exception e){
    e.printStackTrace();
}

But I've also heard that catching generic exceptions is not a good coding practice

public class DataAnalyzerTester {
    /**
     * @param args args[0] stores the filename
     * @exception NoSuchElementException if user attempts to access empty list element
     * @exception ArithmeticException if user attempts to divide by 0
     * @exception Arrayindexoutofboundsexception if user supplied less than 3 arguments
     * @exception IOException problems with creating and writing files
     * @exception RuntimeException if user attempts to pass empty list to constructor
     */
    public static void main(String[] args) {

    try{
        //some code

    } catch (NoSuchElementException e) {
        System.out.println("Accessing element that does not exist: " + e.toString());
    } catch (ArithmeticException e) {
        System.out.println("Division by zero: " + e.toString());
    } catch (Arrayindexoutofboundsexception e) {
        System.out.println("Please supply a command line arguement that specifies your file path: " + e.toString());
    } catch (IOException e) {
        System.out.println("Other IO errors: " + e.toString());
    } catch (RuntimeException e) {
        System.out.println(e.toString());
    } 
    }
}

I wonder if there is a better and clearer way to catch multiple exceptions

Solution

First, unless you have a good reason, you will never catch runtimeException, exception or throwable These will capture most of the things thrown, and throwable will capture everything, even things you don't want to capture, such as outofmemoryerror

Second, avoid catching runtime exceptions unless they directly hinder the critical operation of the program (but seriously, if someone sees you find a NullPointerException, they have the right to call you on you.) The only exceptions you need to pay attention to are those you need to deal with In your exception list, the only thing you should disturb is IOException The rest are the results of insufficient testing or hasty coding; Those should not occur during the uptime of your application

Third, in Java 7, if exceptions are mutually exclusive, you can execute multi catch statements for exceptions The linked example explains it well, but if you encounter code that throws both IOException and sqlexception, you can handle it like this:

try {
    // Dodgy database code here
catch (IOException|sqlException ex) {
    logger.log(ex);
    throw ex;
}

This will clean up some things because you don't have a clumsy and huge chain of exceptions

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>