Last active
July 21, 2016 11:36
-
-
Save loxal/bd6b992c94ebe1d6a889 to your computer and use it in GitHub Desktop.
Interview: Generics and Type Reification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.SQLException; | |
public class Mocker<T extends Exception> { | |
private void pleaseThrow(final Exception e) throws T { | |
throw (T) e; | |
} | |
public static void main(String[] args) { | |
try { | |
new Mocker<RuntimeException>().pleaseThrow(new SQLException()); | |
} catch (final SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What happens with this Java code?
(1) Throws
ClassCastException
becauseSQLException
is not instanceofRuntimeException
(2) Compilation fails because no
SQLException
is thrown(3) The program prints the stacktrace of the newly thrown
SQLException
(4) Compilation fails because we cannot cast
SQLException
toRuntimeException