1

An attempt to use generics with any class that extends Throwable produces an error saying "Generic class may not extend 'java.lang.Throwable'".

Sample code:

public class GenericException<T> extends Exception { }  // Compilation Error

I am not able to understand why generics cannot be used inside classes that extend Throwable. Could someone explain?

shmosel
  • 49,289
  • 6
  • 73
  • 138
Ali
  • 1,442
  • 1
  • 15
  • 29
  • Please search SO for similar question first: http://stackoverflow.com/questions/501277/why-doesnt-java-allow-generic-subclasses-of-throwable – Dmitry Gorkovets Feb 20 '17 at 07:17
  • Did not think about searching for Throwable instead searched for Exception which did not bring up any results. – Ali Feb 20 '17 at 07:20
  • Ali, Compilation error says "Generic class may not extend 'java.lang.Throwable'". – Dmitry Gorkovets Feb 20 '17 at 07:32
  • I should have searched for the exact error message. I was using a java 8 certification book that mentioned that "It is not possible to have generic exception classes;" So i based my search on that. Although the book does give an example that extends Throwable, I extended Exception class instead of Throwable. I agree with you that this is a duplicate post. The link that @shmosel put in the post answers the question. – Ali Feb 20 '17 at 07:40

0 Answers0