Java: exceptions in signature of overriden method

An item from ‘funny things’ bag. Consider a class with method throwing unchecked exception:

class Foo {
	void fizz() throws Exception {

Then extend it and override the fizz method:

class Bar extends Foo {
	void fizz() {

Notice no Exception in Bar::fizz method. It’s ok. It is allowed to remove exceptions thrown by super class (but you can’t add new ones). However the result is that the same method has different signatures in both classes, and this leads to funny behaviour like below:

Bar bar = new Bar();
bar.fizz(); //Ok

((Foo)bar).fizz(); // Error: Unhandled exception: java.lang.Exception

One may get a little surprised.

Written by Michał Łoński on 24 July 2018