leo*_*loy 27
方法可以抛出几个异常中的一个.例如:
public void dosomething() throws IOException, AWTException {
// ....
}
Run Code Online (Sandbox Code Playgroud)
这标志着该方法可以最终抛出一个这两个例外(以及任何的unchecked异常)的.你不能(用Java或任何语言AFAIK)同时抛出两个例外,这没有多大意义.
您还可以抛出一个嵌套的 Exception,它包含另一个异常对象.但这几乎不能算作"抛出两个例外",它只是表示由两个异常对象(通常来自不同层)描述的单个异常情况.
JRL*_*JRL 20
我想你可以创建一个包含已捕获异常列表的异常并抛出该异常,例如:
class AggregateException extends Exception {
List<Exception> basket;
}
Run Code Online (Sandbox Code Playgroud)
aio*_*obe 19
你不能抛出两个例外.即你不能做这样的事情:
try {
throw new IllegalArgumentException(), new NullPointerException();
} catch (IllegalArgumentException iae) {
// ...
} catch (NullPointerException npe) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
您可以使用cause-constructor 嵌套异常来执行以下操作:
try {
Exception ex1 = new NullPointerException();
// Throw an IllegalArgumentException that "wraps" ex1
throw new IllegalArgumentException(ex1);
} catch (IllegalArgumentException iae) {
// handle illegal argument...
throw iae.getCause(); // throws the cause (the NullPointerException)
}
Run Code Online (Sandbox Code Playgroud)
关于链式异常的好文章:Programming.Guide:链式异常
一个例外可以抑制另一个异常.
try {
Exception ex1 = new NullPointerException();
// Throw an IllegalArgumentException that "suppresses" ex1
IllegalArgumentException ex2 = new IllegalArgumentException();
ex2.addSuppressed(ex1);
throw ex2;
} catch (IllegalArgumentException iae) {
// handle illegal argument...
... iae.getSuppressed() ... // get hold of the suppressed exceptions
}
Run Code Online (Sandbox Code Playgroud)
关于抑制异常的好文章:Programming.Guide:抑制异常
抛出多个异常没有意义,因为您不能有多个错误(错误可能有多个原因,但任何时候都不会有多个错误).
如果您需要跟踪原因,可以链接异常:
} catch (Exception ex) {
throw new RuntimeException("Exc while trying ...", ex);
}
Run Code Online (Sandbox Code Playgroud)
这些都可以通过getCause().
我见过一个模式,其中一个自定义异常在内部存储其他异常(不记得,为什么他们这样做了),但它就像:
public class ContainerException extends Exception {
private List<Exception> innerExeptions = new Arrayist<Exception>();
// some constructors
public void add(Exception e) {
innerExceptions.add(e);
}
public Collection<Exception> getExceptions() {
return innerExceptions;
}
}
Run Code Online (Sandbox Code Playgroud)
它像这样使用:
try {
// something
} catch (ContainerException ce) {
ce.add(new RunTimeException("some Message");
throw ce; // or do something else
}
Run Code Online (Sandbox Code Playgroud)
稍后在代码中,评估容器异常并将其转储到日志文件中.
要在 Java 中抛出多个异常,您首先必须将每个异常抑制为一个自定义异常,然后抛出相同的自定义异常。请检查下面的代码片段以实现相同的目的。
public class AggregateException extends Exception {
public void addException(Exception ex){
addSuppressed(ex);
exception = true;
}
}
public class AnyClass{
public AggregateException aggExcep = new AggregateException();
public void whereExceptionOccurs(){
try{
//some code
}catch(Exception e){
aggExcep.addException(e);
//throw aggExcep;
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用相同的引用 aggExcep 在任何你想要的地方(显然在 catch 块内)调用方法 addException 抑制任何异常。最后,在您想要的任何地方使用 'throw' 关键字显式地抛出 aggExcep。
这
void addSuppressed(Throwable 异常)
是 Throwable 类的预定义方法,它将指定的异常附加到为了传递此异常而被抑制的异常。
| 归档时间: |
|
| 查看次数: |
45600 次 |
| 最近记录: |