Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should we use throws keyword in Java? [duplicate]

throws keyword is used only for checked exception. It instructs the caller to use try catch block to except all the listed exceptions by throws keyword.

Since we know what kind of checked exception might occur in our module, then:

  1. Why don't we use try catch block inside the module to handle the checked exceptions?
  2. Can we handle checked exceptions inside the module using try-catch block?
  3. If answer of (2) is YES, then Why we are forcing the caller to except those exceptions using throws keyword, when we could except the same inside the module itself?

In that way we need not to manually except the exceptions every time the method is called.

like image 379
Pran Kumar Sarkar Avatar asked May 25 '19 09:05

Pran Kumar Sarkar


People also ask

When should we use throws in Java?

The throws keyword in Java is used to declare exceptions that can occur during the execution of a program. For any method that can throw exceptions, it is mandatory to use the throws keyword to list the exceptions that can be thrown.

Why we are using throws keyword?

The throws keyword indicates what exception type may be thrown by a method. There are many exception types available in Java: ArithmeticException , ClassNotFoundException , ArrayIndexOutOfBoundsException , SecurityException , etc. Syntax: throw is followed by an object (new type)

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.

Why to use throws keyword if we have try catch block?

Answer: The “throws” keyword is used to declare the exception with the method signature. The throw keyword is used to explicitly throw the exception. The try-catch block is used to handle the exceptions thrown by others.


2 Answers

  1. It is all about how to recover from an exception. What should e.g. java.lang.File do when the file does not exist? As it doesn't know what would be the best for the callee, it lets the callee handle this case
  2. Of course you can handle exceptions in your module if its clear how said exceptions should be handled. If the handling is dependent on the callee or context, let the calling function decide
  3. Should be clear by now
like image 161
roookeee Avatar answered Oct 03 '22 13:10

roookeee


Let me use FileInputStream::new throwing FileNotFoundException as an example to clear up your misunderstanding.

So for example we have some code like this:

FileInputStream fis = new FileInputStream("/some/path/to/file.txt");

That might throw a FileNotFoundException, and you are saying that,

FileInputStream obviously knows that it is going to throw a FileNotFoundException, so why does it not handle it itself?

Because FileInputStream does not know how to handle the exception!

Depending on the situation, there are lots of ways to handle a FileNotFoundException:

  • If the file path comes from user input, you might ask the user to enter another file path
  • You might display an error message
  • You might not do anything and just let it crash

All of the above could be completely sensible options depending on the situation. How is a FileInputStream going to know about your situation? It's not!

That's why it's saying, with a throws clause:

I'm throwing these exceptions, handle it yourself.

like image 26
Sweeper Avatar answered Oct 03 '22 15:10

Sweeper