Search code examples
javaruntimechecked-exceptions

Why are Runtime Exceptions "unchecked" in Java?


Why does it make sense to have Runtime Exceptions UnChecked (as opposed to if they were Checked)?


Solution

  • If you didn't you would have to have try/catch blocks every time you accessed an array element, did a division operation and many other common scenarios.

    To put it another way, imagine this code:

    Map map = ...
    int i = ...
    (int[])map.get("foo")[3] = 2334 / i;
    

    would have to check for ClassCastException, ArrayIndexOutofBoundsException, ArithmeticException, UnsupportedOperationException and NullPointerException just off the top of my head.

    With Java the issue isn't unchecked exceptions. Checked exceptions are a highly controversial subject. Some say this was largely an experiment with Java and in practice they don't work but you will find plenty of people who argue they are good.

    No one is arguing unchecked exceptions are bad however.