Questions tagged [multi-catch]

multi-catch refers to new functionality in Java 7 that allows developers to catch multi exceptions type in one catch block.

In Java prior to version 7 it was frequently necessary to write repetitive code of the form

} catch (Exception ex) {
  logger.error(ex);
  throw ex;
} catch (SecondException ex) {
  logger.error(ex);
  throw ex;
}

Now you can use this equivalent form

} catch (Exception | SecondException ex) {
  logger.error(ex);
  throw ex;
}
29 questions
850
votes
11 answers

Can I catch multiple Java exceptions in the same catch clause?

In Java, I want to do something like this: try { ... } catch (/* code to catch IllegalArgumentException, SecurityException, IllegalAccessException, and NoSuchFieldException at the same time */) { someCode(); } ...instead…
froadie
  • 79,995
  • 75
  • 166
  • 235
14
votes
1 answer

Why does multi-catch feature in Java require exceptions to be final?

The Oracle documentation on the multi-catch feature added to Java 7 states that the exception parameter in the catch clause is implicitly final. My question is: what's the point of such restriction? Because i can't seem to find a single crucial…
Semisonic
  • 1,152
  • 8
  • 21
13
votes
3 answers

Determining compile-time multicatch exception type

I built something I don't really understand - I don't know how it works. I've familiarized myself with this multicatch explaination article. Consider these two exceptions and code: public class MyException1 extends Exception { // constructors,…
Dariusz
  • 21,561
  • 9
  • 74
  • 114
13
votes
2 answers

Maven project Error: Diamond/multicatch operator not supported in -source 1.5

I can't build my maven java web application, because of the following two errors: diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator) multi-catch statement is not supported in -source 1.5 (use…
MeesterPatat
  • 2,671
  • 9
  • 31
  • 54
11
votes
1 answer

Is there an official name for Java 7's combined / multi-catch block?

Upon discussing the multiple-catch / combined catch block here with ambiguity between the terms "multiple catch block," meaning the Java 7 feature: try { .. } catch (ExceptionA | ExceptionB ex) { .. } and "multiple catch blocks," meaning literally,…
FThompson
  • 28,352
  • 13
  • 60
  • 93
10
votes
2 answers

Java bug when combining lambdas and multi-catch clauses?

import java.io.*; import java.net.*; public class Test { public static void main(String[] arguments) throws Exception { Runnable runnable = () -> { try { throwException(); …
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
9
votes
7 answers

Not able to use Multi Catch from Java Effectively

I really want to use features from Java-1.7. One of this feature is "Multi-Catch". Currently I have the following code try { int Id = Integer.parseInt(idstr); TypeInfo tempTypeInfo = getTypeInfo(String.valueOf(Id)); …
M.S.Naidu
  • 2,239
  • 5
  • 32
  • 56
9
votes
3 answers

Why can not Java multi-catch deal with types that is related by subclassing?

Here is a piece of code that shall not compile: void multiCatch() { try { throwIOFile(); } // FileNotFoundException extends IOException, hence this // does not compile ("alternatives" related by sub classing): catch…
Martin Andersson
  • 18,072
  • 9
  • 87
  • 115
7
votes
3 answers

Can't define multi-catches in IntelliJ

I work with JDK 7 in Intellij 12.0.4. When I try to create a multi-catch block it get a "multi-catches are not supported at this language level" error. I found this question but the answer doesn't work for me. This was the answer: Click on the File…
Avi
  • 21,182
  • 26
  • 82
  • 121
5
votes
3 answers

When to use multi-catch and when to use rethrow?

I'm very uncertain about this two topics. I know that i should use multi-catch for exceptions which need to handle the same way. But for what purpose do i really need something like that. private void something(String name) throws IOException,…
LaBlum
  • 61
  • 5
5
votes
6 answers

order in multi-catch exception handler

I know since Java 7 you can use multi-catch but I wonder if the order of exceptions in it matters like in previous versions of java? E.g I put in Exception and then SQLException and IOException ? try { // execute code that may throw 1 of the…
Dodi
  • 2,201
  • 4
  • 30
  • 39
2
votes
2 answers

Using multi-catch Exception type in constructor

Suppose I need to combine 2 apis which throw Exceptions on basically every method. The code usually looks cluttered like this: class MultiApi{ Api1 api1; Api2 api2; //I led the caller handle problems Api2Data doSomethingOrThrow()…
Rafael T
  • 15,401
  • 15
  • 83
  • 144
2
votes
2 answers

WebApplicationException and NotFoundException in multi-catch

So my question is this : Basically I've a method with a try-catch block where I'm catching a WebApplicationException (javax.ws.rs.WebApplicationException) but my method is throwing NotFoundException (com.sun.jersey.api.NotFoundException) . so I…
CodePlorer
  • 153
  • 1
  • 15
2
votes
3 answers

reference type of exception issue in multi-catch block

e is of type Exception but prints Exception1 in below code: class Exception1 extends IOException {void info(){}} class Exception2 extends Exception {} class TestMultiCatch { public static void main(String args[]) { try { int…
mustafa1993
  • 541
  • 1
  • 5
  • 17
2
votes
2 answers

Undefined method in multiple-exception-catch

I have two classes inheriting from java.lang.Exception. They both have a method with the same signature void a(){...}. They both can be thrown in a code block. If I do: catch (SubException1 | SubException2 e) { e.a(); } Then it…
pHneutre
  • 1,091
  • 3
  • 12
  • 29
1
2