0

I am experimenting with exceptions and i want to ask when it is possible to handle multiple exceptions in one handler and when it is not?

For example i wrote the following code which combines two exceptions (FileNotFoundException OutOfMemoryError) and the program runs properly without any error. Al thought the handling is not so relevant with the functionality of the code i chose them just to see when i can combine multiple exceptions in on handler :

import java.io.FileNotFoundException; import java.lang.OutOfMemoryError; public class exceptionTest { public static void main(String[] args) throws Exception { int help = 5; try { foo(help); } catch (FileNotFoundException | OutOfMemoryError e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static boolean foo(int var) throws Exception { if (var > 6) throw new Exception("You variable bigger than 6"); else return true; } } 

But when i choose different type of exceptions the compiler gives me error . For example when i choose IOException and Exception i have the error the exception is already handled " :

import java.io.IOException; import java.lang.Exception; public class exceptionTest { public static void main(String[] args) throws Exception { int help = 5; try { foo(help); } catch (IOException | Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static boolean foo(int var) throws Exception { if (var > 6) throw new Exception("You variable bigger than 6"); else return true; } } 

So why is this happening ? Why in one occasion i can use multiple exception in handler and in the other not ? Thank you in advance.

14
  • Do you have your own class called IOException? Commented Jun 15, 2014 at 20:09
  • 2
    OutOfMemoryError belongs in the java.lang.Error hierarchy. Different than java.lang.Exception. Take a look at the different classes: IOException & OutOfMemoryError Commented Jun 15, 2014 at 20:10
  • have a look at programcreek.com/2009/02/… Commented Jun 15, 2014 at 20:12
  • 3
    Your code works fine in both cases. Commented Jun 15, 2014 at 20:12
  • 1
    Works fine for me (JDK 1.7). No warning, no error. And, in fact, both are subclasses of Throwable. Commented Jun 15, 2014 at 20:18

1 Answer 1

1

You are getting the message because IOException is a subclass of Exception. Therefore, if an IOException were thrown, it would be caught by a catch (Exception e) statement, so catching it as an IOException is redundant.
The first example works because neither FileNotFoundException nor OutOfMemoryError is a subclass the other.

However, you can catch sub-classed exceptions using the separate catch statement:

try{ // code that might throw IOException or another Exception } catch (IOException e) { // code here will execute if an IOException is thrown } catch (Exception e) { // code here will execute with an Exception that is not an IOException } 

If you do this, please note that the subclass must come first.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes McLoving , now i understood why is the problem . Chris Tarazi already gave me the answer which explains it cleary " @Dioskouros I think the problem lies with IOException being a child of Exception. So there's no reason for you to handle a specific exception (IOException) and a broad exception (Exception) at the same time." Thank you all guys for your comments and your interest !
You're welcome. There are a lot of complications when dealing with exceptions and inheritance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.