3

When trying to use

catch( EntityExistsException | PersistenceException eeEx)

The compiler complains :

No exception of type Object can be thrown; an exception type must be a subclass of Throwable

I am using eclipse luna with compiler set to java 8 for the project

sepp2k
  • 363,768
  • 54
  • 674
  • 675
osama yaccoub
  • 1,884
  • 2
  • 17
  • 47

3 Answers3

3

I just ran into this and it was driving me crazy.
I was creating a multi-catch exception handler by adding URISyntaxException to and existing catch.
Eclipse complained with

No exception of type Object can be thrown; an exception type must be a subclass of Throwable

I finally noticed that I had not added the corresponding import statement so eclipse didn't know the type of the newly added exception.
It would be nice if eclipse would have complained with

URISyntaxException cannot be resolved to a type

which is what is says when the exception is by itself.
The error you get when it's part of a multi-catch is non-intuitive.

Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
SamBeroz
  • 91
  • 1
  • 7
0

Sounds like one of those missing 'extends Exception'.

I don't think it has anything to do with "Mutliple Exception Handling", JDK8 will definitely compile code like that.

Are both of those actually extend exceptions/are subs of throwable?

Jenya G
  • 504
  • 3
  • 8
  • OK. Can you verify that it is the actual class that you expect at the runtime? It's possible that at the runtime it gets overridden. Use Class.forName for all those in question, if you develop in eclipse ctrl+shit+t search by that type to see what matches in your project classpath. But find all instances of it at the runtime anyway, even if search by type shows only 1 place. One more thing, can you put generic Exception/RuntimeException object in there and see if it runs? If it does, then it's something what I said above. – Jenya G Nov 22 '15 at 23:21
0

In eclipse, just make sure you import all the exceptions in the catch clause

catch(SomeException se | SomeException1 se1 | SomeException2 se2){
    doSomething();
}

Missing import statements will show error like this.

Lucky
  • 16,787
  • 19
  • 117
  • 151