0
\$\begingroup\$

I am creating program which will parse .csv file and I am using FileReader and Scanner classes from java API.

FileReader throws FileNotFoundException, IOException.

Scanner's method hasNextLine() and nextLine() throw IllegalStateException and NoSuchElementException.

Should I use 1 try and 4 multiple catch blocks? Or try block nested in another try block(which has 2 catch blocks) with 2 catch blocks?

Code 1:

import java.io.*;
import java.util.Scanner;
import java.util.NoSuchElementException;
import java.lang.IllegalStateException;

public class App {
    public static void main(String[] args) {
        try (FileReader file = new FileReader("Doc.csv")) {
            Scanner sc = new Scanner(file);
            while (sc.hasNextLine()) {
                System.out.println(sc.nextLine());
            }
        } catch (IllegalStateException e) {
            System.out.println(e);
        } catch (NoSuchElementException e) {
            System.out.println(e);
        } catch (FileNotFoundException e) {
            System.out.println(e);
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}

Code 2:

import java.io.*;
import java.util.Scanner;
import java.util.NoSuchElementException;
import java.lang.IllegalStateException;

public class App {
    public static void main(String[] args) {
        try (FileReader file = new FileReader("Doc.csv")) {
            Scanner sc = new Scanner(file);
            try {
                while (sc.hasNextLine()) {
                    System.out.println(sc.nextLine());
                }
            } catch (IllegalStateException e) {
                System.out.println(e);
            } catch (NoSuchElementException e) {
                System.out.println(e);
            }
        } catch (FileNotFoundException e) {
            System.out.println(e);
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}

By the way, I know that System.out.println(e) is not best practice for logging exception. So, please don't write about it

\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. \$\endgroup\$ – Toby Speight Jul 9 at 11:45
  • \$\begingroup\$ Welcome to Code Review. Please note that unlike Stack Overflow, in order to give the best possible advice Code Review needs to look at concrete code in a real context. We also prefer general questions like "Any recommendations for how to improve this code in any way?" rather than specific questions about "How should I write my try-catch statement, A or B?" (where the correct answer often is C - as it was here as well). In this particular case I considered that you essentially have a complete example project here, so I'm letting this question pass. \$\endgroup\$ – Simon Forsberg Jul 10 at 11:23
4
\$\begingroup\$

If you are not going to handle each exception differently just use a multicatch:

public static void main(String[] args) {
    try (FileReader file = new FileReader("Doc.csv")) {
        Scanner sc = new Scanner(file);
        while (sc.hasNextLine()) {
            System.out.println(sc.nextLine());
        }
    } catch (IllegalStateException | NoSuchElementException | IOException e) {
        System.out.println(e);
    }
}

The FileNotFoundException is not catched since alternatives in a multi-catch statement cannot be related by subclassing (FileNotFoundException is a subclass of IOException).

\$\endgroup\$
  • \$\begingroup\$ Thanks. So, it is okey to have 1 try and catch(with multiple exceptions) block if I want to handle all exceptions with the same method? \$\endgroup\$ – Genjik Jul 9 at 11:27
  • \$\begingroup\$ this is what I meant by "do the same processing with all exceptions" \$\endgroup\$ – Sharon Ben Asher Jul 9 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.