Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use throws in a Java method declaration?

So I thought I had a good basic understanding of exception-handling in Java, but I was recently reading some code that gave me some confusion and doubts. My main doubt that I want to address here is when should a person use throws in a Java method declaration like the following:

    public void method() throws SomeException     {          // method body here     } 

From reading some similar posts I gather that throws is used as a sort of declaration that SomeException could be thrown during the execution of the method.

My confusion comes from some code that looked like this:

     public void method() throws IOException      {           try           {                BufferedReader br = new BufferedReader(new FileReader("file.txt"));           }           catch(IOException e)           {                System.out.println(e.getMessage());           }      }

Is there any reason that you would want to use a throws in this example? It seems that if you are just doing basic exception-handling of something like an IOException that you would simply need the try/catch block and that's it.

like image 754
jbranchaud Avatar asked Dec 08 '10 21:12

jbranchaud


People also ask

What is the purpose of using throws clause in a method declaration in Java?

The throws clause in a method declaration serves two purposes: It tells the compiler which exceptions are thrown so that the compiler can report uncaught (checked) exceptions as errors. It tells a programmer who is writing code that calls the method what exceptions to expect.

How do you decide if an exception should be thrown in a method?

An exception should be thrown when a function experiences a failure, i.e., an error. A function is a unit of work, and failures should be viewed as errors or otherwise based on their impact on functions.

What is the advantage of throws in Java?

The Java throws keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception. So, it is better for the programmer to provide the exception handling code so that the normal flow of the program can be maintained.

Are throws necessary?

No, it means if you catch the exception that gets thrown, such that it cannot bubble up the stack, then you do not need to declare it (e.g., try { throw new Exception(); } catch (Throwable) {} ).


1 Answers

If you are catching an exception type, you do not need to throw it, unless you are going to rethrow it. In the example you post, the developer should have done one or another, not both.

Typically, if you are not going to do anything with the exception, you should not catch it.

The most dangerous thing you can do is catch an exception and not do anything with it.

A good discussion of when it is appropriate to throw exceptions is here

When to throw an exception?

like image 188
hvgotcodes Avatar answered Oct 11 '22 05:10

hvgotcodes