We can declare methods that throw an exception (or error) of unknown type. Example (of method with type parameter in throws clause): interface Task< E extends Exception > { void run() throws E ; } Can such a method throw an object of the unknown exception (or error) type?
• create a new exception (or error) and throw it • catch the exception (or error) of an invoked operation and re-throw it • propagate the exception (or error) of an invoked operation As we must not create objects of an unknown type the first possibility is not an option. Since type parameters must not appear in catch clauses, we cannot catch and therefore not re-throw an exception of an unknown type. At best, we can propagate an exception of unknown type that is raised by any of the invoked operations. Example (throwing an object of unknown type): final class CleanUp< E extends Exception , T extends Task
Related Questions
- We can declare methods that throw an exception (or error) of unknown type. Example (of method with type parameter in throws clause): interface Task< E extends Exception > { void run() throws E ; } Can such a method throw an object of the unknown exception (or error) type?
- Given a method that doesn declare any exceptions, can I override that method in a subclass to throw an exception?
- Why do I get an error (CS1006) when trying to declare a method without specifying a return type?