What does “Throws” do and how is it helpful? [dupl

2019-01-22 04:17发布

问题:

This question already has an answer here:

  • The throws keyword for exceptions in Java 4 answers

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?

回答1:

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.



回答2:

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.


回答3:

Higher up yeah, some method will have to catch the exception that gets thrown. Just like you said, it is the try-catch blocks that manage them.

There is one exception though, and that is the RuntimeException, for which you do not need to declare the throws exception part. RuntimeException(and all of its subclasses) are called unchecked exceptions, after which you usually can not recover.



回答4:

Throws is a mechanism to throw the exception to the calling method. This is generally used to throw the exception to a level where it can be handled.

A practical example is a gui based application with some backend logic. If there is a problem in the backend, you may want to show a proper message to the user. So from your backend you can throw the exception upto your UI classes and then can display a message accordingly.



回答5:

Java programs throw exception whenever it is occured.However,there will be times when you want to manually throw exception,for this throws keyword is used.

For example: class Abc { public static void main(String args[]) throws IOException{ }}