Java “投掷”有什么作用,它有什么帮助?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18491020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "Throws" do and how is it helpful?
提问by PotWashMike
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.
我是 Java 的新手,刚刚遇到一个在方法中使用“抛出”关键字的教程。我对此做了一些研究,但仍然没有真正理解它。
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?
从我目前看到的情况来看,它告诉编译器在该特定方法中可能会抛出某个异常。为什么我们需要告诉编译器这个?我已经在我的方法中仅使用 try-catch 语句制作了许多程序并且它运行得很好 - 当然是这些 try-catch 语句管理异常,对吧?
采纳答案by John Gowers
You can manage an exception withina method using try
and catch
as you say. In that case, you do not need to use throws
. For example:
您可以使用和如您所说的那样在方法中管理异常。在这种情况下,您不需要使用. 例如:try
catch
throws
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.
但是假设您有一千个方法,所有这些方法都可能抛出一个SpaceInvadersException
. 那么你最终将不得不编写所有复杂的错误处理代码一千次。当然,你总是可以创建一个ErrorHandler
类与dealWithSpaceInvadersException()
方法,你可以从他们打电话,但你仍然可以用一千卡try
-catch
块。
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 itmight throw a SpaceInvadersException
. This is done using the throws
keyword, like this:
相反,您告诉编译器这数千个方法中的每一个都可能抛出一个SpaceInvadersException
. 然后任何调用这些方法之一的方法都需要处理错误本身,要么使用try
- catch
,要么告诉编译器它可能会抛出一个SpaceInvadersException
. 这是使用throws
关键字完成的,如下所示:
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).
在这种情况下,您需要通知编译器myMethod
可能会抛出SpaceInvadersException
. 这意味着您不能在不以某种方式处理异常的情况下调用该方法(try
-catch
或throws
在调用方法上使用关键字)。如果throws
不存在,您可以在不进行任何异常处理的情况下调用该方法,并获得程序中任何地方都没有处理的异常(这会很糟糕)。
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.
由于避免代码重复总是更好,因此通常最好将错误处理交给 a try
-catch
在更高级别的函数中,而不是在所有低级别方法中单独处理它。这就是这种机制存在的原因。
回答by Zavior
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.
更高,是的,某些方法必须捕获抛出的异常。就像你说的,管理它们的是 try-catch 块。
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.
但是有一个异常,那就是RuntimeException,您不需要声明 throws 异常部分。RuntimeException(及其所有子类)称为未经检查的异常,之后通常无法恢复。
回答by Andy Thomas
The throws
keyword declares that the exception can be thrown outof the method.
该throws
关键字声明该异常可能抛出出来的方法。
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.
您还可以使用catch
块来捕获方法内的异常。如果你捕获它并且不重新抛出它,那么它不会被抛出到方法之外。
A throws
declaration allows compile-time verification that a method either:
甲throws
声明允许编译时间验证的是,方法之一:
- Catches the exceptions it throws, including those from the methods it calls.
- Or declares them, so that its callers can make the same check.
- 捕获它抛出的异常,包括来自它调用的方法的异常。
- 或者声明它们,以便其调用者可以进行相同的检查。
回答by Juned Ahsan
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.
Throws 是一种向调用方法抛出异常的机制。这通常用于将异常抛出到可以处理的级别。
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.
一个实际的例子是一个基于 gui 的应用程序,带有一些后端逻辑。如果后端出现问题,您可能希望向用户显示正确的消息。因此,从您的后端,您可以将异常抛出到您的 UI 类,然后可以相应地显示一条消息。
回答by Eduardo Savrin
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.
Java 程序会在发生异常时抛出异常。但是,有时您需要手动抛出异常,因为throws
使用了这个关键字。
For example:
class Abc {
public static void main(String args[]) throws IOException{
}}
例如:
class Abc {
public static void main(String args[]) throws IOException{
}}