0

I have following code:

1 public static byte[] foo() throws IOException {

    ...

2   try (DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream())) {
3        wr.write(stringObj.getBytes("UTF-8"));
4        wr.flush();
5    } catch (Exception e) {
6        throw e;
7    }

    ...

}

The interesting part of this code is, it's allowing parent class to be caught by a child, I am not sure why this is the case, though I also added following few more lines of code to understand it:

Try 1 (no compile-time error):

1 public static byte[] foo() throws IOException {

    ...

2   try (DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream())) {
3        wr.write(stringObj.getBytes("UTF-8"));
4        wr.flush();

5         throw new ArrayIndexOutOfBoundsException();

6    } catch (Exception e) {
7        throw e;
8    }

    ...

}

Try 2 (compile time error at line 8 but line 6 worked):

1 public static byte[] foo() throws IOException {

    ...

2   try (DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream())) {
3        wr.write(stringObj.getBytes("UTF-8"));
4        wr.flush();
5    } catch (Exception e) {
6        throw e;
7    }

8    throw new Exception();
    ...

}

Also, I know downcasting is allowed when the type is same at runtime, but then try 1 line 5 worked (maybe at runtime it will fail if ArrayIndexOutOfBoundsException occurs)?

Downcasting is allowed when there is a possibility that it suceeds at run time:

Object o = getSomeObject();

String s = (String) o; // this is allowed because o could reference a String

Community
  • 1
  • 1
Vishrant
  • 15,456
  • 11
  • 71
  • 120
  • 2
    The compiler is smart enough to know that what you catch can only be an IOException (or a runtime exception). – JB Nizet Feb 23 '18 at 16:59
  • @JBNizet are you saying that if the line `throw new ArrayIndexOutOfBoundsException();` was replaced with `throw new Exception();`, then the compiler will complain? – smac89 Feb 23 '18 at 17:04
  • Yup that seems to be the case – smac89 Feb 23 '18 at 17:09
  • @smac89 can you please provide us with any reference which can justify that. – Vishrant Feb 23 '18 at 17:13
  • @Sotirios Delimanolis I appreciate your prompt response to this answer, but I am aware of the concept that you pointed out to be duplicate. But whatever you marked does not seems to be relevant to this question. If so, please elaborate. – Vishrant Feb 23 '18 at 17:15
  • The `IOException` in your question is misleading. Remove it, it doesn't contribute anything to the behavior you see. Then you'll understand the duplicate. – Sotirios Delimanolis Feb 23 '18 at 17:16
  • @SotiriosDelimanolis exactly that is my point, its miss leading and that why I need the reason, removing it `IOException` and making it similar to other does not answer. It does not make sense why first example compiled successfully but not try 2. – Vishrant Feb 23 '18 at 17:20
  • You're fixating on the `IOException`. Change it to `InterruptedException`, change it to `Throwable`, change it to any other checked exception. It doesn't matter. It's not relevant. The behavior is the same and explained in the duplicates. – Sotirios Delimanolis Feb 23 '18 at 17:23
  • Sure, thanks the other reference you just add seems to be duplicate of this and now it makes sense to close this question. – Vishrant Feb 23 '18 at 17:26
  • I don't need to provide any other reference than what you already have. Take the code you have and replace `throw new ArrayIndexOutOfBoundsException()` with `throw new Exception()`, and you will see that the compiler throws an exception. – smac89 Feb 23 '18 at 17:39
  • @smac89 I did and it compiled successfully, and that is why I am confused. :) – Vishrant Feb 23 '18 at 17:51
  • @Vishrant, is your method still declared like `public static byte[] foo() throws IOException`? – smac89 Feb 23 '18 at 17:52
  • that is correct. – Vishrant Feb 23 '18 at 17:52
  • I'm not sure tbh. This is what I did: https://gist.github.com/anonymous/ec9b6e54f37430c7e8c89c02c693da88. If you remove the comment comment for the other exception, there is a compiler error. Maybe I have to try creating something that throws IOException? I will try that too and see if it behaves differently – smac89 Feb 23 '18 at 18:06
  • Yup just tried with something similar to what you have: https://gist.github.com/anonymous/63a5f7385d4eb17bd8191fa3cf278b65. If it helps, here is my compiler version `java version "1.8.0_161" Java(TM) SE Runtime Environment (build 1.8.0_161-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)` – smac89 Feb 23 '18 at 18:13

0 Answers0