Order of throws exceptions in Java

I am writing code for reading files into Java.

I have a main method that throws three Exceptions. I am wondering what would the impact would be if I switched the order of writing the exceptions in the same line of public static void main(String[] args).

public class SwitchTester {

    private static Scanner pathInput;

    public static void main(String[] args) throws IOException, NoSuchElementException, IllegalStateException{

I use the code inside the main method. The order of exceptions I use is IOException, NoSuchElementException, IllegalStateException in this code. Would it matter to keep the same order in the main line? Would it matter to keep throws in the main line or not at all?

    try{         
        pathInput = new Scanner(path);
    }catch(IOException ioException){
        System.err.println("Error opening file. Terminating.");
        System.exit(1);
    }

    System.out.printf("%-10s%-15s%-15s%10s%n", "Port", "Destination",
             "Source", "Data-type");

    //readRecords
    try{
        while(pathInput.hasNext()){ //while there is more to read, display record contents
            System.out.printf("%-10s%-15s%-15s%10s%n", pathInput.next(), 
                    pathInput.next(), pathInput.next(), pathInput.next());
        }
     }catch(NoSuchElementException elementException){
         System.err.println("File improperly formed. Terminating.");
     }catch(IllegalStateException stateException){
         System.err.println("Error reading from file. Terminating.");
     }

I would appreciate your answer! Thanks!

Answers


The order of exceptions in your throws clause has no functional impact on calling code. Nor does omitting unchecked throwables (those that derive from Error or RuntimeException). You are however required to declared any checked exceptions; they are not optional.

JLS 8.4.6 documents the requirements for the throws clause:

It is permitted but not required to mention unchecked exception classes (ยง11.1.1) in a throws clause.

In general, what you want to be careful about is whether you want to declare a superclass in the throws clause, or discrete subclasses. For example, it is generally bad practice to say throws Exception instead of e.g. throws FileNotFoundException because it puts a higher burden on callers that want to handle thrown exceptions intelligently.


The order in which the exceptions is listed in the throws clause of the method declaration totally doesn't matter. It definitely does not have to have any relation to where you throw the exceptions in the code.

Assuming this is a main method called only from the JVM, and none of your code is actually calling this method, this is one time where it would be perfectly fine to save yourself some typing and write throws Exception. (On the other hand if you refactor the main method so that it calls separate methods that handle different functions then you would want those methods to be more descriptive of what checked exceptions they throw.)


When you catching the exceptions you should always catch the most specific first and then the most generic,but if you are throwing the exceptions type, you do not need to catch them.

For more info of exception takea look at hierarchy-of-exception and this package-tree


Need Your Help

cc1plus: error: unrecognized command line option "-std=c++11" with g++

c++ c++11 compiler-errors g++ std

I'm trying to compile using g++ and either the -std=c++11 or c++0x flags.

Foundation 6 topbar animation with motion-ui

javascript html css css-animations zurb-foundation-6

Using Foundation 6, I try to get a responsive topbar that involves an animation on toggle (small screens).