I wrote the following code recently; it uses a lot of exception handling. I think it makes the code look very unreadable. I could shorten the code by catching generic exception, such as
catch (Exception e){ e.printStackTrace(); } but I also heard that catching generic exception 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 would like to know if there is any better and cleaner way to catch multiple exceptions.