Java 从一种方法抛出多个异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22229171/
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
Throwing multiple exceptions from one method
提问by user3388925
How do you throw more than one exception at once from one method? Example:
如何从一种方法一次抛出多个异常?例子:
public void doA() throws Exception1, Exception2{
throw new Exception1("test1");
throw new Exception2("test2");
}
How to make something like this work?
如何使这样的工作?
Edit : one condition throws both Exception1 and Exception2. Possible? This is just a demo file to test throwing exceptions.
编辑:一个条件同时抛出异常 1 和异常 2。可能的?这只是一个测试抛出异常的演示文件。
回答by Luiggi Mendoza
You should check if something is not right in the method to throw the Exception
. Here's a sample:
您应该检查抛出Exception
. 这是一个示例:
public void doA() throws Exception1, Exception2 {
if (<some unexpected condition>) {
throw new Exception1("test1");
}
if (<another unexpected condition>) {
throw new Exception2("test2");
}
//rest of the implementation...
}
If you mean how to throw several exceptions at the same time, that's not possible since throwing a exception will break the execution of the method (similar to a return
). You can only throw one Exception
at a time.
如果您的意思是如何同时抛出多个异常,那是不可能的,因为抛出异常会中断方法的执行(类似于 a return
)。一次只能扔一个Exception
。
回答by user2424380
If your question was how can you throw more than one exception from a method at the same timethen the answer is you just can't. After the first exception is thrown the control exits this method and the Exception is rolling in it's parent method. If there is nothing which catches it then it goes to it's parent method and so on...
如果您的问题是如何同时从一个方法中抛出多个异常,那么答案就是您不能。在抛出第一个异常后,控件退出此方法,异常在其父方法中滚动。如果没有任何东西可以捕获它,那么它会转到它的父方法等等......
回答by Cascader
I don't think it is possible to throw more than one exception at-once.
我认为一次抛出多个异常是不可能的。
However, depending on the requirement, you can throw a nested exception, when you need to pass contextually the (sic) second exception:
但是,根据要求,当您需要在上下文中传递(原文如此)第二个异常时,您可以抛出嵌套异常:
throw new Exception("My exception", new Exception("The cause of My Exception"));
回答by rdllopes
Throwing exception is an one-way transfer of control to another line of code (like jump in assembly language). That means after the throwing statement the program should be executing necessarily another line of code outside that scope (at first calling code in stack which handle that exception).
抛出异常是将控制权单向转移到另一行代码(如汇编语言中的跳转)。这意味着在 throwing 语句之后,程序必须执行该范围之外的另一行代码(首先调用堆栈中处理该异常的代码)。
回答by Hari
You can throw only one exception at a time. You cannot do what you are asking for. Instead, think about using custom exceptions in your code, and use them, depending on the situation (I am not sure about the reason why you need to throw two exceptions) :
一次只能抛出一个异常。你不能做你所要求的。相反,考虑在您的代码中使用自定义异常,并根据情况使用它们(我不确定您需要抛出两个异常的原因):
class CustomException extends Exception
{
//Parameterless Constructor
public CustomException() {}
//Constructor that accepts a message
public CustomException(String message)
{
super(message);
}
}
and then throw it when you need to:
然后在需要时抛出它:
public void doA() throws Exception1, Exception2{
throw new CustomException("test1, test2");
}