Behavior of executing a method call with a checked exception at runtime without catching it

Viewed 62

Suppose I have caller Class named let's say A in a jar file a.jar that calls a method let say B.b() in a separate jar file b.jar.

Until there everything is ok.

Now B package developers are adding a checked Exception to B.b() which becomes something like B.b() throws Exception and redeploy the b.jar file. The a.jar file is kept intact, untouched so the A package developers do not notice the compile error requiring to catch the exception immediately.

What will happen at runtime ? More precisely how the JVM deals with that. From my experience, but surprisingly it works smoothly in the case where no exceptions are actually thrown at runtime. So it looks that the method resolution is performed even if signature does not match at the exception level. On the other hand, if B.b() actually throws an exception I have no idea of what will happen. Is this behaviour specified somewhere in the JVM or Java language specification or it is up to the JVM implementor or maybe even configurable with some JVM flags options ?

Thanks for your clarifications

1 Answers

Exceptions are not part of a method's signature, at least, for identifying which method is intended. Furthermore, at the VM level, there is no such thing as a 'checked exception'. You can throw anything you like, even if the signature of your method doesn't declare it. HOw do you think lombok's SneakyThrows, or scala works?

Related