在 Java 中抛出多个异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2912565/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:24:53  来源:igfitidea点击:

Throwing multiple exceptions in Java

javaexception

提问by ajay

Is there any way to throw multiple exceptions in java?

有没有办法在java中抛出多个异常?

回答by aioobe

You can't throw two exceptions. I.e. you can't do something like:

你不能抛出两个异常。即你不能做这样的事情:

try {
    throw new IllegalArgumentException(), new NullPointerException();
} catch (IllegalArgumentException iae) {
    // ...
} catch (NullPointerException npe) {
    // ...
}

Alternative 1: Exception A is caused byexception B

备选方案 1:异常 A 是异常 B引起的

You could nestexceptions using the cause-constructorto do the following:

您可以使用原因构造函数嵌套异常以执行以下操作:

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)
}

Good article on chained exceptions: Programming.Guide: Chained Exceptions

关于链式异常的好文章:Programming.Guide: Chained Exceptions

Alternative 2: Use suppressedexception

备选方案 2:使用抑制异常

One exception can suppressanother exception.

一个异常可以抑制另一个异常。

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
}

Good article on suppressed exceptions: Programming.Guide: Suppressed Exceptions

关于抑制异常的好文章:Programming.Guide: Suppressed Exceptions

回答by Aaron Digulla

Throwing more than a single exception doesn't make sense because you can't have more than a single error (the error can have multiple reasons but there can't be more than a single error at any time).

抛出多个异常没有意义,因为您不能有多个错误(错误可能有多种原因,但在任何时候都不能超过一个错误)。

If you need to keep track of the reasons, you can chain exceptions:

如果您需要跟踪原因,您可以链接异常:

} catch (Exception ex) {
    throw new RuntimeException("Exc while trying ...", ex);
}

These are available via getCause().

这些可通过getCause().

回答by jjnguy

You can have the possibility of throwing multiple different exceptions. For example:

您可以抛出多个不同的异常。例如:

if (obj == null)
    throw new NullPointerException();

if (some other case)
    throw new IllegalArgumentException();

if (this == this)
    throw new IOException();

This code may throw multiple different exceptions, but this can never happen at the same time.

这段代码可能会抛出多个不同的异常,但这永远不会同时发生。

回答by JRL

I suppose you could create an exception containing a list of caught exceptions and throw that exception, e.g.:

我想你可以创建一个包含捕获异常列表的异常并抛出该异常,例如:

class AggregateException extends Exception {
    List<Exception> basket;
}

回答by leonbloy

A method can throw one of several exceptions. Eg:

一个方法可以抛出几个异常之一。例如:

 public void dosomething() throws IOException, AWTException {
      // ....
 }

This signals that the method can eventually throw oneof those two exceptions (and also any of the unchecked exceptions). You cannnot (in Java or in any language AFAIK) throw simultaneously two exceptions, that would not make much sense.

这个信号,该方法可以最终抛出一个这两个例外(以及任何的unchecked异常)。您不能(在 Java 或任何语言 AFAIK 中)同时抛出两个异常,这没有多大意义。

You can also throw a nestedException, which contains inside another one exception object. But that would hardly count that as "throwing two exceptions", it just represents a single exception case described by two exceptions objects (frequently from different layers).

您还可以抛出一个嵌套的Exception,其中包含另一个异常对象。但这几乎不能算作“抛出两个异常”,它只是表示由两个异常对象(通常来自不同层)描述的单个异常情况。

回答by Andreas Dolk

I've seen a pattern, where a custom exception internally stores other exceptions (can't remember, why they did it), but it was like:

我见过一种模式,其中自定义异常在内部存储其他异常(不记得,他们为什么这样做),但它就像:

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;
  }
}

and it was used like this:

它是这样使用的:

try {
  // something
} catch (ContainerException ce) {
  ce.add(new RunTimeException("some Message");
  throw ce; // or do something else
}

Later in the code, the container exception was evaluated and dumped to a log file.

在代码的后面,容器异常被评估并转储到日志文件中。

回答by Michael Uzquiano

The pattern described by Andreas_D is definitely useful whenever you're handling, say, the server-side compilation of a user-provided file and want to report back the errors.

Andreas_D 描述的模式在您处理用户提供的文件的服务器端编译并希望报告错误时绝对有用。

For example, you might have a CompilationException which gets generated if the resource fails to compile. Compiling might mean any number of things. For example, you might evaluate the text inside of a file that an end user uploads, parse out tokens, check for syntax errors and determine whether the file is valid. At the end, the thing is either valid or it is invalid and you want to hand back a proper CompilationException to propagate back up the call stack.

例如,如果资源无法编译,您可能会生成 CompilationException。编译可能意味着许多事情。例如,您可以评估最终用户上传的文件中的文本、解析令牌、检查语法错误并确定文件是否有效。最后,该事物要么有效,要么无效,并且您想要交回适当的 CompilationException 以传播回调用堆栈。

Like Andreas describes, you can have an add() method that lets you add compilation issues to your exception. Those issues don't have to be Exceptions themselves, but that's up to you. It often helps to stick to a single exception framework so that you can use the same validation logic in multiple places.

就像 Andreas 描述的那样,您可以使用 add() 方法将编译问题添加到异常中。这些问题本身不一定是异常,但这取决于您。坚持使用单个异常框架通常会有所帮助,以便您可以在多个地方使用相同的验证逻辑。

At any rate, what you want is a single CompilationException falling back through the call stack because that tells the framework that the thing did not compile. If anyone up the chain wants to know why, then they can get at the underlying issues with a call to, say, getCauses().

无论如何,您想要的是单个 CompilationException 通过调用堆栈回退,因为它告诉框架该事物未编译。如果链上的任何人想知道原因,那么他们可以通过调用 getCauses() 来了解潜在问题。

It's also useful from a UI perspective. That information sticking around on the Exception can be properly handled before going back over the wire so that you can give your end user some information as to why the compilation failed.

从 UI 的角度来看,它也很有用。在通过网络返回之前,可以正确处理留在异常上的信息,以便您可以向最终用户提供有关编译失败原因的一些信息。

回答by log

there is a way for a method to throw mutiple exeptions, but not at one time. e.g. when compilation fails for some reason your method can only throw one exception. if you have to cover different opportunities you might declare you method to throw the parent class of all exceptions "Exception". so, if you declare a method to throw an Exception in general, an exception of any type can be thrown by this method.

有一种方法可以抛出多个异常,但不是一次。例如,当由于某种原因编译失败时,您的方法只能抛出一个异常。如果您必须涵盖不同的机会,您可能会声明您的方法来抛出所有异常“异常”的父类。因此,如果您声明一个通常会抛出异常的方法,则此方法可以抛出任何类型的异常。

for example:

例如:

public static void main(String[] args) throws Exception
{
  getClipboard();    // throws an UnsupportedFlavorException
  initIOActivity();  // throw an IOException
}

i don't know what you actually needed to know but maybe this helps. although it has passed a lot of time since your post ^^

我不知道你真正需要知道什么,但也许这会有所帮助。虽然自从你的帖子已经过去了很多时间^^

greetings

你好

回答by SAHIL SRIVASTAVA

To throw multiple exceptions in Java you'll first have to suppress each exception into one customized exception and then throw the same customized exception. Please check the below code snippet to achieve the same.

要在 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;
        }  
    }
}

Call the method addException with the same reference aggExcep wherever you want to(Inside the catch block obviously) suppress any exception. And at the end explicitly throw aggExcep using 'throw' keyword where ever you want to.

使用相同的引用 aggExcep 在任何你想要的地方(显然在 catch 块内)调用方法 addException 抑制任何异常。最后,在您想要的任何地方使用 'throw' 关键字显式地抛出 aggExcep。

The

void addSuppressed(Throwable exception)

void addSuppressed(Throwable 异常)

is a predefined method of Throwable class which appends the specified exception to the exceptions that were suppressed in order to deliver this exception.

是 Throwable 类的预定义方法,它将指定的异常附加到为了传递此异常而被抑制的异常。

回答by Kathan Kashiparekh

I am unsure if you are asking whether multiple exceptions can be thrown at a time or we can have a code to handle multiple exceptions at a time. I will try to answer both of these. This is my first answer on StackOverflow so pardon any errors.

我不确定您是在问是否可以一次抛出多个异常,或者我们可以有一个代码来一次处理多个异常。我将尝试回答这两个问题。这是我在 StackOverflow 上的第一个答案,所以请原谅任何错误。

1) If you want to throw multiple exceptions at a time,I think you can't do that. Consider an analogous situation. When you are solving a Math questions and reach a point where you are dividing by 0, there is only one error AT THIS POINT of time and that is dividing by zero. So I guess you can just throw one error for a given statement. However there can exist many statements in your try catch block each of which might throw a different error.

1)如果你想一次抛出多个异常,我认为你不能那样做。考虑一个类似的情况。当您解决数学问题并达到除以 0 的程度时,此时只有一个错误,即除以零。所以我想你可以只为给定的语句抛出一个错误。但是,在您的 try catch 块中可能存在许多语句,每个语句都可能引发不同的错误。

2) IF you want to handle/catch multiple errors there are two ways to do it. i) Before Java 7:

2)如果您想处理/捕获多个错误,有两种方法可以做到。i) 在 Java 7 之前:

`try{
     ...
     //some method/action that can be a cause of multiple errors,say X and Y
     ...
 }catch(XException e){
      //Do something if exception X arises.
 }catch(YException e){
      //Do something if exception Y arises.
 }
`

ii) After Java 7,you have the multi catch feature.

ii) 在 Java 7 之后,你有多重捕捉功能。

try{
     ...
     //some method/action that can be a cause of multiple errors,say X and Y
     ...
 }catch(XException|YException e){
   // Take action appropriate to both types of exception.
  ...
 }

I believe this will solve your doubt. This being my first answer,all suggestions are welcome!

相信这会解开你的疑惑。这是我的第一个答案,欢迎所有建议!