Java,在没有 try 块的情况下使用 throw
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30010037/
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
Java, using throw without try block
提问by Martin
For the following method, is it okay to throw the exception when it isn't inside a 'try' block?
对于下面的方法,当它不在“try”块内时是否可以抛出异常?
public void setCapacity(x) throws CapacityExceededException {
if (x > 10) throw new CapacityExceededException(x);
else this.x = x;
}
回答by underdog
Yes it is Ok to throw an exception when it isn't inside a try block. All you have do is declare that your method throws an exception. Otherwise compiler will give an error.
是的,当它不在 try 块内时抛出异常是可以的。您所要做的就是声明您的方法抛出异常。否则编译器会报错。
public void setCapacity(x) throws CapacityExceededException {
if (x > 10) throw new CapacityExceededException(x);
else this.x = x;
}
You don't even have to do that if your CapacityExceededException
extends Runtime Exception.
如果您CapacityExceededException
扩展了运行时异常,您甚至不必这样做。
public void setA(int a) {
this.a = a;
if(a<0) throw new NullPointerException();
}
This code won't give any compiler error. As NPE is a RuntimeException.
此代码不会给出任何编译器错误。因为 NPE 是一个 RuntimeException。
When you throw an exception the exception will be propagated to the method which called setCapacity()
method. That method has to deal with the exception via try catch or propagate it further up the call stack by rethrowing it.
当您抛出异常时,异常将传播到调用setCapacity()
方法的方法。该方法必须通过 try catch 处理异常,或者通过重新抛出异常将其传播到调用堆栈上。
回答by akhil_mittal
Yes it is ok. You simply need to add throws clause for method in case it throws a checked exception. There is no restriction of a try clause for throwing an exception. For example if you have a stack and called pop()
method and size is zero you can check it and throw an exception. You may need to catch the exception wherever you plan to invoke this method. Otherwise this uncaught exception will move up the invocation stack and will be finally handled by JVM and it will print stack trace on System.err
stream. We can also specify our own handler if we want:
是的,没关系。您只需要为方法添加 throws 子句,以防它抛出已检查的异常。对抛出异常的 try 子句没有限制。例如,如果您有一个堆栈并调用pop()
方法并且大小为零,您可以检查它并抛出异常。您可能需要在计划调用此方法的任何地方捕获异常。否则,这个未捕获的异常将向上移动调用堆栈,最终由 JVM 处理,并将在System.err
流上打印堆栈跟踪。如果需要,我们还可以指定我们自己的处理程序:
public class CustomUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
// Write the custom logic here
}
}
You do not even need to mention exception in throws if it is a RunTimException
.
如果它是一个RunTimException
.
回答by Stephen C
... is it okay to throw the exception when it isn't inside a 'try' block?
...当它不在'try'块内时可以抛出异常吗?
Of course1it is!.
当然是1!
Indeed ... that is the most common way to throw exceptions.
确实……这是最常见的抛出异常的方式。
Indeed 2 ... if we didn't use exceptions like that, they would be (almost) redundant since we could (almost) replace them with "break to label" and a local variable to hold the exception information.
确实 2 ...如果我们不使用这样的异常,它们将(几乎)是多余的,因为我们可以(几乎)用“break to label”和一个局部变量来替换它们来保存异常信息。
1 - Provided that the exception is unchecked2, or you declared it in the method signature.
1 - 前提是异常未被检查2,或者您在方法签名中声明了它。
2 - That is RuntimeException
, Error
or descendants of the same.
2 - 即RuntimeException
,Error
或相同的后代。
回答by Zizouz212
Just add a throws
declaration:
只需添加一个throws
声明:
public int getCapacity() throws CapacityExceededException {
// Something goes here
}
Otherwise, you'll get compiler warnings. This will get the method to throw the exception. However, whenever you execute the method, you will have to use the try
block. This is because an exception could be thrown by execution of the method.
否则,您将收到编译器警告。这将获得抛出异常的方法。但是,无论何时执行该方法,都必须使用该try
块。这是因为执行该方法可能会引发异常。
try {
getCapacity()
} catch (CapacityExceededException e) {
// Do Something
}