Checked exceptions are a failed expriment in language design. They force you to have extremely leaky abstractions and dirty code. They should be avoided as much as possible.
As for your points:
1) Checked exceptions make the code dirty, and no less unpredictable because they show up everywhere.
2) How is a checked exception being shown to the user any better? The only real difference between checked and unchecked exceptions is technical and affects only the source code.
3) Ever heard of stack traces? They tell you exactly where the exception was thrown, no matter whether it's checked or unchecked. Actually, checked exceptions tend to be worse for debugging because they're often wrapped which leads to longer and uglier stack traces, or even lost atogether because the wrapping was done wrong.
There are two kinds of exceptions: those that occur "normally" and are typically handled very close to where they occur, and those that are reallly exceptional and can be handled generically in a very high layer (just abort the current action and log it / show an error).
Checked exceptions were an attempt to put this distinction into the language syntax at the point the exceptions are defined. The problems with this are
- The distinction is really up to the caller, not the code that throws the exception
- It's completely orthogonal to the semantic meaning of an exception, but tying it to the class hierarchy forces you to mix the two
- The whole point of exceptions is that you can decide at what level to catch them without risking to lose an error silently or having to pollute the code at intermediate levels or ; checked exceptions lose that second advantage.
Difference between java.lang.RuntimeException and ...
Can you add throws RuntimeException to method signature?
java - Is it not a good practice to handle runtime exceptions in the code? - Software Engineering Stack Exchange
Java runtime error 'EXCEPTION_ACCESS_VIOLATION (0xc0000005)'
A fatal error has been detected by the Java Runtime Environment:
EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ff8249dd9ac, pid=6056, tid=6308
JRE version: Java(TM) SE Runtime Environment (8.0_25-b18) (build 1.8.0_25-b18) Java VM: Java HotSpot(TM) 64-Bit Server VM (25.25-b02 mixed mode windows-amd64 compressed oops) Problematic frame: C [ig75icd64.dll+0x55d9ac]
Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
An error report file with more information is saved as: C:\Users\edthr\AppData\Roaming.minecraft\hs_err_pid6056.log
If you would like to submit a bug report, please visit: http://bugreport.sun.com/bugreport/crash.jsp The crash happened outside the Java Virtual Machine in native code. See problematic frame for where to report the bug.
Java HotSpot(TM) 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release
More on reddit.comVideos
Checked exceptions are a failed expriment in language design. They force you to have extremely leaky abstractions and dirty code. They should be avoided as much as possible.
As for your points:
1) Checked exceptions make the code dirty, and no less unpredictable because they show up everywhere.
2) How is a checked exception being shown to the user any better? The only real difference between checked and unchecked exceptions is technical and affects only the source code.
3) Ever heard of stack traces? They tell you exactly where the exception was thrown, no matter whether it's checked or unchecked. Actually, checked exceptions tend to be worse for debugging because they're often wrapped which leads to longer and uglier stack traces, or even lost atogether because the wrapping was done wrong.
There are two kinds of exceptions: those that occur "normally" and are typically handled very close to where they occur, and those that are reallly exceptional and can be handled generically in a very high layer (just abort the current action and log it / show an error).
Checked exceptions were an attempt to put this distinction into the language syntax at the point the exceptions are defined. The problems with this are
- The distinction is really up to the caller, not the code that throws the exception
- It's completely orthogonal to the semantic meaning of an exception, but tying it to the class hierarchy forces you to mix the two
- The whole point of exceptions is that you can decide at what level to catch them without risking to lose an error silently or having to pollute the code at intermediate levels or ; checked exceptions lose that second advantage.
My view is that what type of exception is thrown is dependant on what your code is doing.
I throw checked exceptions when I expect they can happen quite often (users entering dodgy data for example) and I expect the calling code to handle the situation.
I throw unchecked/runtime exceptions (not as often as checked) when I have a rare situation which I do not expect the calling code to handle. An example might be some sort of weird memory related error which I never expect to occur. Unchecked are the sorts of errors that you expect to bring down the application.
No exception should appear in front of a user without some level of support. Even if it's just a "please cut and paste this error dump to an email". Nothing is more annoying to a user than being told there is an error, but being given no details or action they can take to initiate it being fixed.
My guess is that the philosophy you mention comes from one of two sources:
- Lazy programmers trying to avoid doing work.
- Or developers who have had to support code which went the other way. i.e. over-error-handling. The sort of code that contains large amounts of exception handling, much of which does nothing, or even worse, is used for flow control and other incorrect purposes.
Generally RuntimeExceptions are exceptions that can be prevented programmatically. E.g NullPointerException, ArrayIndexOutOfBoundException. If you check for null before calling any method, NullPointerException would never occur. Similarly ArrayIndexOutOfBoundException would never occur if you check the index first. RuntimeException are not checked by the compiler, so it is clean code.
EDIT : These days people favor RuntimeException because the clean code it produces. It is totally a personal choice.
In Java, there are two types of exceptions: checked exceptions and un-checked exceptions. A checked exception must be handled explicitly by the code, whereas, an un-checked exception does not need to be explicitly handled.
For checked exceptions, you either have to put a try/catch block around the code that could potentially throw the exception, or add a "throws" clause to the method, to indicate that the method might throw this type of exception (which must be handled in the calling class or above).
Any exception that derives from "Exception" is a checked exception, whereas a class that derives from RuntimeException is un-checked. RuntimeExceptions do not need to be explicitly handled by the calling code.
I'm studying java at school my professor gave us an exercise where you have to create a method whose signature is: "public static double calculates(double o1, double o2, char op) throws InputMismatchException". I'm studying exceptions online and I found some sites that tell you you shouldn't add throws RuntimeException because the problem isn't handled the same way. I'm too afraid to ask my professor because that would be implying e taught us wrong all the time.
It depends.
For example, Integer#parseInt throws NumberFormatException (which is a RTE) if the provided string can't be parsed. But surely you don't want your app to crash just because the user wrote "x" to a text field that was for integers? And how do you know whether the string can be parsed, unless you try to parse it first? So in this case, the RTE is just an error signal that should cause some kind of error message. One could argue that it should be a checked exception, but what can you do - it isn't.
NullPointerExceptions are usually the sign of a missing null check. So, instead of catching it like this, you should add the apropriate null check to be sure to not throw the exception.
But sometimes, it is appropiate to handle RunTimeExceptions. For example, when you cannot modify the code to add the null check at the appropriate place, or when the exception is something other than a NullPointerException.
Your example of handling exceptions is terrible. Doing so, you lose the stack trace and precise information about the problem. And you are actually not solving it as you will probably trigger another NullPointerException in a different place, and get misleading information about what happened and how to solve it.