Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "Throws" do and how is it helpful? [duplicate]

I am new to Java and have just came across a tutorial which uses the"Throws" keyword in a method. I have done a little research into this but still do not really understand it.

From what I have seen so far, it is telling the compiler that a certain exception may be thrown in that particular method. Why do we need to tell the compiler this? I have made many programs using merely a try-catch statement in my methods and it has worked just fine - surely it is these try-catch statements which manage exceptions, right?

like image 967
PotWashMike Avatar asked Aug 28 '13 14:08

PotWashMike


People also ask

What does throws in a method do?

The throws keyword provides information about the exceptions to the programmer as well as to the caller of the method that throws the exceptions. The throws keyword allows exceptions to be propagated in the call stack. When a method declares that it throws an exception, it is not required to handle the exception.

What is the importance of throw?

Throwing strengthens these key areas of development: Integrated Movement. Hand-eye Coordination. Learning About Gravity.

What is throw and throws and how do you use?

Throw is a keyword which is used to throw an exception explicitly in the program inside a function or inside a block of code. Throws is a keyword used in the method signature used to declare an exception which might get thrown by the function while executing the code.

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.


2 Answers

You can manage an exception within a method using try and catch as you say. In that case, you do not need to use throws. For example:

public void myMethod()
{
  try {
    /* Code that might throw an exception */
  }
  catch (SpaceInvadersException exception) {
    /* Complicated error handling code */
  }
}

But suppose you had a thousand methods, all of which might throw a SpaceInvadersException. Then you would end up having to write all the complicated error handling code a thousand times. Of course, you could always create an ErrorHandler class with a dealWithSpaceInvadersException() method that you could call from them, but you'd still be stuck with a thousand try-catch blocks.

Instead, you tell the compiler that each of these thousand methods could throw a SpaceInvadersException. Then any method that calls one of these methods needs to deal with the error itself, either by using a try-catch, or by telling the compiler that it might throw a SpaceInvadersException. This is done using the throws keyword, like this:

public void myMethod() throws SpaceInvadersException
{
  /* Code that might throw an exception */
}

public void callingMethod()
{
  try {
    myMethod();
  }
  catch (SpaceInvadersException exception) {
    /* Complicated error handling code */
  }
}

In that case, you need to inform the compiler that myMethod could throw a SpaceInvadersException. This means that you can't call that method without dealing with the exception in some way (try-catch or using the throws keyword on the calling method). If throws weren't there, you could call the method without doing any exception handling, and get an exception that wasn't dealt with anywhere in the program (which would be bad).

Since it is always better to avoid code duplication, it is normally better to palm off your error handling to a try-catch in a much higher level function than it is to deal with it separately in all of the low level methods. That is why this mechanism exists.

like image 85
John Gowers Avatar answered Sep 21 '22 17:09

John Gowers


The throws keyword declares that the exception can be thrown out of the method.

You can also use a catch block to catch an exception inside the method. If you catch it and don't rethrow it, then it's not thrown out of the method.

A throws declaration allows compile-time verification that a method either:

  • Catches the exceptions it throws, including those from the methods it calls.
  • Or declares them, so that its callers can make the same check.
like image 35
Andy Thomas Avatar answered Sep 21 '22 17:09

Andy Thomas