Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can still use generic methods, like this:</p> <pre><code>public class SomeException { private final Object target; public SomeException(Object target) { this.target = target; } public &lt;T&gt; T getTarget() { return (T) target; } } .... catch (SomeException e) { Integer target = e.getTarget(); } </code></pre> <p>I do agree with Cristian's answer above. While the accepted answer is technically correct (insofar as it references the JVM specs), Cristian Vasile's answer is one that qualifies and even challenges the limitation.</p> <p>There are least two arguments that I noted in answers to this question that I do not agree with and that I will rebut. If the arguments in these answers were correct, we could use these arguments to attack generics in other contexts where they are used successfully today.</p> <hr> <p>The first argument states that we can't use this:</p> <pre><code>catch (Exception&lt;T1&gt; e) {} </code></pre> <p>because the JVM doesn't know how to work with <code>Exception&lt;T1&gt;</code>. This argument would seem to also attack this use of generics, on the basis that the JVM doesn't know how to use <code>List&lt;T1&gt;</code>:</p> <pre><code>List&lt;T1&gt; list; </code></pre> <p>The argument, of course, forgets that the compiler performs type erasure and so the JVM doesn't need to know how to handle <code>Exception&lt;T1&gt;</code>. It can simply handle <code>Exception</code>, just like it handles <code>List</code>.</p> <p>Of course, we could never handle <code>catch(Exception&lt;T1&gt; e)</code> and <code>catch(Exception&lt;T2&gt; e)</code> in the same try/catch because of type erasure, but then again, that's no worse than with method arguments or return values today: we don't handle <code>myMethod(List&lt;T1&gt;)</code> and <code>myMethod(List&lt;T2&gt;)</code> today either... (I reiterate this aspect in the second rebuttal below.)</p> <hr> <p>A second argument goes as follows. We don't allow this:</p> <pre><code>catch (Exception&lt;T1&gt; e) {} </code></pre> <p>because this wouldn't work:</p> <pre><code>catch (Exception&lt;T1&gt; e) {} catch (Exception&lt;T2&gt; e) {} </code></pre> <p>OK, then why not disallow this:</p> <pre><code>interface MyInterface { Comparable&lt;Integer&gt; getComparable(); } </code></pre> <p>because this <strong>does not work</strong>:</p> <pre><code>interface MyInterface { Comparable&lt;Integer&gt; getComparable(); Comparable&lt;String&gt; getComparable(); } </code></pre> <p>or this:</p> <pre><code>interface MyInterface { void setComparable(Comparable&lt;Integer&gt; comparable); } </code></pre> <p>because this <strong>does not work</strong>:</p> <pre><code>interface MyInterface { void setComparable(Comparable&lt;Integer&gt; comparable); void setComparable(Comparable&lt;String&gt; comparable); } </code></pre> <p>In other words, why not disallow generics in most cases?</p> <p>This second argument forgets that, although we couldn't possibly allow different generic constructs that that erase to the same non-generic construct in these contexts, we can still do the next best thing and <em>allow generics as long as the types don't erase to the same type</em>. That's what we do with method parameters: we allow you to use generics, but complain as soon as we detect duplicate signatures after type erasure. Well, we could have done about the same thing with exceptions and catch blocks...</p> <hr> <p>In conclusion, I would expand on Cristian's answer. Instead of allowing generic exception classes <em>and</em> using the raw types in <code>catch</code> blocks:</p> <pre><code>class MyException&lt;T&gt; {} ... catch (MyException e) { // raw </code></pre> <p>Java could have gone the whole way without problems:</p> <pre><code>class MyException&lt;T&gt; {} ... catch (MyException&lt;Foo&gt; e) { </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload