What does it mean when the main method throws an exception?

后端 未结 8 1337
轮回少年
轮回少年 2021-01-01 09:31

I\'m reviewing a midterm I did in preparation for my final exam tomorrow morning. I got this question wrong, but there\'s no correct answer pointed out, and I neglected to a

相关标签:
8条回答
  • 2021-01-01 10:06

    The main method is not catching any exceptions, instead it handles the FileNotFoundException by throwing it to the source which invoked the main method.

    The system runtime launches the JVM classes, one specific class among the JVM classes invokes the main method.

    The handling for the main method's throws is at the mercy of the JVM classes in that case.

    • You can read about it in the Java language specification provided by Oracle.
    • Additionally you can view the source code for some of the JVMs available out there, going that path though takes you away to other programming languages,OpenJdk.

    I thought of sharing my small humbled research crust in that topic, hope it helps curious ones :)

    0 讨论(0)
  • 2021-01-01 10:08

    With only the declaration of main(), it is impossible to say which answer is objectively correct. Any of the statements could be true, depending on the definition of the method.

    1. The main method is designed to catch and handle all types of exceptions.

    2. The main method is designed to catch and handle the FileNotFoundException.

    Both of the above statements are true of the following:

    public static void main(String[] args) throws FileNotFoundException {
        while (true) {
            try {
                doSomething();
            }
            catch (Exception e) {}
        }
    }
    

    The declared exception is never thrown by main(), but that is not an error; just pointless and misleading.

    1. The main method should simply terminate if the FileNotFoundException occurs.

    2. The main method should simply terminate if any exception occurs.

    Both of the above statements are true of the following:

    public static void main(String[] args) throws FileNotFoundException {
        try {
            doSomething();
        }
        catch (Exception e) {
            return;
        }
    } 
    

    Of course, we can guess at the intention of the question based on what a decent and reasonable programmer might intend to communicate with this method signature. Which would be that they intend for the method to throw FileNotFoundException, and necessarily handle other checked Exceptions. We can also reasonably assume that "handle" does not just mean "process", but specifically that it will not (re-)throw such an exception.

    These assumptions immediately rule out #1 and #2.

    The remaining question is whether "simply terminate" includes throwing an exception, or only an explicit return/System.exit(). In the former case, both of #3 and #4 could still be true:

    public static void main(String[] args) throws FileNotFoundException {
        try {
            doSomething();
        }
        catch (FileNotFoundException fnfe) {
            throw fnfe;
        }
        catch (Exception e) {
            return;
        }
    }
    

    In the latter case, neither #3 nor #4 can be true while also satisfying the assumption that main() will throw FileNotFoundException.


    In sum, the options are not worded well. If I had to pick an answer, it would be #3 based on the logic of MartinV's answer. My assumption would be that the word "should" in #3 was an unfortunate choice by the professor, and that something like "may" would have been a better option. It would also have been a good idea to use more precise language than "simply terminate" (and, arguably, "handle").

    0 讨论(0)
  • 2021-01-01 10:11

    Answer is number 4,

    4.- The main method should simply terminate if any exception occurs.

    The throws clause only states that the method throws a checked FileNotFoundException and the calling method should catch or rethrow it. If a non-checked exception is thrown (and not catch) in the main method, it will also terminate.

    Check this test:

    public class ExceptionThrownTest {
    
        @Test
        public void testingExceptions() {
    
            try {
                ExceptionThrownTest.main(new String[] {});
            } catch (Throwable e) {
                assertTrue(e instanceof RuntimeException);
            }
    
        }
    
        public static void main(String[] args) throws FileNotFoundException {
    
            dangerousMethod();
    
            // Won't be executed because RuntimeException thrown
            unreachableMethod();
    
        }
    
        private static void dangerousMethod() {
            throw new RuntimeException();
        }
    
        private static void unreachableMethod() {
            System.out.println("Won't execute");
        }
    }
    

    As you can see, if I throw a RuntimeException the method will terminate even if the exception thrown is not a FileNotFoundException

    0 讨论(0)
  • 2021-01-01 10:14

    Although an answer has already been chosen for this question, I think that the correct answer to your test is number 3:

    1. The main method should simply terminate if the FileNotFoundException occurs.

    Because it says should, not would.
    I mean, the code in the method can catch any exception, both checked and unchecked, and either, swallow, recover from or rethrow it — or it could not catch them at all.

    But if it catches a FileNotFoundException without ultimately (re)throwing it, it is breaking its own contract: any caller won't see the exception as stated in the method signature, because the method handled it (maybe even the compiler could complain in that case, saying that the exception is never thrown, but I not sure here).

    I think your midterm was posing a question about design rather than about a specific implementation.

    0 讨论(0)
  • 2021-01-01 10:16

    I agree with some other answers that the correct answer to the question is option 3. Option 4 says:

    1. The main method should simply terminate if any exception occurs.

    Note the "any" in this option. Here's an example of code in which an exception occurs, but main() does not terminate:

    public static void main(String[] args) throws FileNotFoundException {
        try {
            methodThatThrowsACheckedException();
        } catch (SomeCheckedException e) {
            // do something to handle this exception
        }
    }
    

    In this code an exception occurs, but the method does not terminate, as it's been setup to handle this exception. If the exception were an uncaught UncheckedException, then the method would terminate, of course. The point of option 4, though, is that any counter-example invalidates it, since it says "any" exception occurs.

    Option 3, however, limits this termination to only occur when the exception in the method's signature is thrown:

    1. The main method should simply terminate if the FileNotFoundException occurs.

    The reason option 3 makes more sense is because code like the following does not make sense in practice:

    public static void main(String[] args) throws FileNotFoundException {
        try {
            methodThatThrowsFileNotFoundException();
        } catch (FileNotFoundException e) {
            // do something to handle this exception
        }
    }
    

    It doesn't make much sense to declare that a method throws an exception, but catch that exception in the method (unless, perhaps, you re-throw it after doing something, in which case option 3 still holds, as the method terminates eventually).

    0 讨论(0)
  • 2021-01-01 10:25

    The answer is both 2 & 3.

    2.The main method is designed to catch and handle the FileNotFoundException. 3.The main method should simply terminate if the FileNotFoundException occurs.

    But if fails to handle the exception even though it is designed to handle it and the programs gets terminated abnormally.

    throws keywords is used to make JVM handle the exceptions which we are lazy to handle, it compiles successfully but shows the exception during runtime(if u handle it in main method then it compiles and runs successfully).

    4.The main method should simply terminate if any exception occurs. Is not correct always because the exceptions which occurs may be handle in the main method.

    1.The main method is designed to catch and handle all types of exceptions.Is incorrect as JVM doesn't handle unchecked exceptions.

    0 讨论(0)
提交回复
热议问题