-4

Why I need to put the throws clause in the main method to handle the exception? It shouldn't be only the try-catch supposed to handle exceptions? Sorry for my english

public static void main(String[] args) throws IOException {
    createFileDude();
}

public static void createFileDude() throws IOException {
    File file = new File("C:\\Users\\User\\Desktop\\Test.txt");
    try {
        System.out.println("Create file>> " + file.createNewFile());
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
}
Elazar
  • 20,415
  • 4
  • 46
  • 67
  • 4
    You're re-throwing the exception in the `catch` block... – Robby Cornelissen Jun 11 '19 at 02:25
  • A better question is why in fact are you calling `throw e;` from within a catch block? In this setting, it really makes little sense. – Hovercraft Full Of Eels Jun 11 '19 at 02:26
  • This is just an example ;-; – Luiz Fernando Jun 11 '19 at 02:29
  • 5
    No, it is the crux of your question and is the complete cause of your problem. Get rid of that nonsense and the problem goes away. By throwing the exception your try/catch doesn't really handle the exception but off-loads it to the next level. – Hovercraft Full Of Eels Jun 11 '19 at 02:31
  • 1
    The try block is in effect stating, "I will handle this exception", and then the throw within the catch is countering this by stating, "no, in fact, I won't handle it" – Hovercraft Full Of Eels Jun 11 '19 at 02:33
  • Possible duplicate of [Exception thrown inside catch block - will it be caught again?](https://stackoverflow.com/questions/143622/exception-thrown-inside-catch-block-will-it-be-caught-again) – Hovercraft Full Of Eels Jun 11 '19 at 02:34
  • There are times that you *might* want to do this, but not here as all it's doing is forcing you to put `throws ....` on the main method. – Hovercraft Full Of Eels Jun 11 '19 at 02:38
  • But when I load the exception to the next level (in this case the main method) why it's gives me a compile error until I use a throws clause in the main method? I understand that I've made mistakes but is proposital because I don't understand what happens after the throws clause being placed on the main method – Luiz Fernando Jun 11 '19 at 02:43

1 Answers1

0

Exception means something went wrong in the 'current method'. Now, the developer can handle it in the catch block of current method, or just tell to the calling method that something went wrong for them to handle.

Like this, eventually the developer can cascade the error back to the operating system. So, it is all about throwing the error back to the calling method or not throwing it back at some point.

In this case, there are two places where you as a developer can decide if the exception message be cascaded back to the OS or not, first is in the method you wrote. As I see you wrote that the exception be thrown, now in main method, you as a developer again have a chance to not throw it to OS (by enclosing in it try-catch but don't throw in catch block), or throw it to OS.

In your example you chose to throw it back to the OS by adding throws clause.

From Java perspective, Java wants the you (the developer) make a conscious decision on any given (checked) exception, and so you'll see compilation errors until you either suppress the exception by not throwing in, or until you actually throw it back to the OS from the 'main' method.

Siva
  • 598
  • 3
  • 11