tags:

views:

144

answers:

6

Hi

Is there any way to throw multiple exception in java.

Thanks

+5  A: 

No, there is no way you could do something like:

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

if that's what you're asking for.

You could however use the cause-constructor to do the following:

try {
    throw new IllegalArgumentException(new NullPointerException());
} catch (IllegalArgumentException iae) {
    // handle illegal argument...
    throw iae.getCause(); // throws the cause (the NullPointerException)
}
aioobe
+3  A: 

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().

Aaron Digulla
Please don't encourage people to catch the base `Exception`. Also, for repackaging exceptions, I a project specific exception base class. This way I can differentiate between acceptable exceptions, ones I have already wrapped and am planning for, and an unexpected exception.
unholysampler
@unholysampler: Agreed.
Aaron Digulla
+2  A: 

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.

jjnguy
+2  A: 

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;
}
JRL
+2  A: 

A method can throw one of several exceptions. Eg:

 public void dosomething() throws IOException,
                               ArrayIndexOutOfBoundsException {
 }

This signals that the method can eventually throw one of 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.

You can also throw a nested Exception - one that contains inside another one exception object), but that would hardly count that as "two exceptions", they just represent a single exception case represented by two exceptions objects (frequently from different layers).

leonbloy
+1  A: 

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.

Andreas_D