0

I know questions like this are everywhere, but I read a lot of things about this, and I still can't understand what the "throws" command do. I will be more specific now:

So, one of the examples I saw was this one, with the following code:

public class CatchThrow {

private static void throwsMethod() throws NumberFormatException {
    String  intNumber = "5A";

    Integer.parseInt(intNumber);
}

private static void catchMethod() {
    try {

        throwsMethod();

    } catch (NumberFormatException e) {
        System.out.println("Convertion Error");
    }

}

public static void main(String[] args) {
    // TODO Auto-generated method stub

    catchMethod();
}

}

Now, if I remove the "throws NumberFormatException" from the "throwsMethod" method, the program will run the same, and will give the same results. Actually, every example with the throws command that I saw did the same, so I can't really understand why use it.

I'm using the Eclipse IDE, version 4.7.2.

Tricolor
  • 133
  • 3
  • You need to read about the distinction between checked and unchecked exceptions in Java – ernest_k May 31 '18 at 20:34
  • 1
    You might want to take a look at this answer: https://stackoverflow.com/a/27763869/7123665 – Riiverside May 31 '18 at 20:34
  • 1
    Possible duplicate of [Differences between Runtime/Checked/Unchecked/Error/Exception](https://stackoverflow.com/questions/3162760/differences-between-runtime-checked-unchecked-error-exception) – Riiverside May 31 '18 at 20:35
  • 1
    Yeah, that's a poor example. There are two kinds of exception - ones where you need the `throws` (checked exceptions), and ones where you don't (unchecked exceptions). A `NumberFormatException` is an unchecked one. – Dawood ibn Kareem May 31 '18 at 20:35
  • https://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html – JB Nizet May 31 '18 at 20:35
  • https://stackoverflow.com/questions/11589302/why-is-throws-exception-necessary-when-calling-a-function – Shashwat Kumar May 31 '18 at 20:43

2 Answers2

0

Normally your function exits at the end of the function or the return statement.

However, a function can also exit when it reaches a throw statement. If the exception subclasses Exception, the caller of the function must surround the function call with a try { } catch { } block. If the exception subclasses RuntimeException you may optionally surround the function call in a try catch block.

If you look at the JavaDoc for NumberFormatException: https://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html will see it subclasses RuntimeException. This means your try-catch block is optional. The difference between the two program is this: With the try-catch block you will get Convertion Error printed to the console, without it, you will see the full stack trace. This is often called "swallowing the exception".

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
0

So basically, if an exception occurs and you don't want to handle that exception there, in that case you use the 'throw' keyword to simply just throw the exception if occurs.

Example: Here, in throwsMethod(), you are not taking care of the Exception Handling i.e. not using the try(), catch() blocks, you are just throwing it if there occurs any Exception. And you will land in catch() block if exception occurs in your throwsMethod().

To get better idea, you should read checked & Unckecked exceptions in Java. For Checked exceptions (happen at compile-time), we use 'throw' keyword and for Unchecked (Run-time), we use try() catch().

Example: NumberFormatException is an Unchecked exception, IOException is a Checked exception.

Read this for reference: https://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/

Keshav Aggarwal
  • 754
  • 6
  • 14