Java – is it effective to handle runtimeexceptions in some cases?
As I have understood from several tutorials, runtimeexceptions should not actually be caught because they should reveal improper usage of methods, especially APIs. Are they correct?
firstDecimalChar = formattedValue.charAt(dotPosition + 1);
If the user enters something like "27", this statement will throw an exception Since this is based on strange user input, I wonder if this exception really looks like a common exception (so to speak) I think this is because in most tutorials (such as Oracle), they classify the checked exceptions as errors caused by invalid input, that is, the wrong path name to the file that should be opened, and they can recover from that error through some corrections or user prompts Also here: people may just set the above variable to zero, because what does it mean when someone inserts "27". – > “27.0”
Is this capture and specification so strict as to be true? Of course, if the position is not within the limit, you can simply check it in advance, but my problem is that if there is such a straight line between the checked and unchecked exceptions I wonder why there are any technical drawbacks to avoiding exceptions as often as possible (some people mention JVM overhead, but does it make sense?)
I think I need more clarification here, because most tutorials don't make me believe that "you shouldn't do this at all" Because I think in this case, the behavior of this exception is similar to an easily recoverable checked exception
Note: in my code, I first check if the string position is valid and don't catch exceptions - so this is not part of the problem:) but this situation makes me busy
Solution
No, I think unchecked exceptions can be caught in some cases
For example, if the user enters a number, I think it's all right
double i; try { i = Double.parseDouble(userInput); } catch (NumberFormatException e) { addValidationError("Entered number is not valid: " + userInput); }
If the exception is unexpected and corresponds to an error in the code, ignore it (or catch it at the top level, such as in handlerequest or any other case, and return a 500 error) On the other hand, if you can foresee throwing an exception (and use ordinary control structures (such as if statements to cover these situations, such as the numberformatexception example above), it will be caught and processed
As it happens, runtimeexceptions usually correspond to programmer errors, and saying "don't catch runtimeexceptions" may be regarded as a good approximation of what is captured and what is captured
When the developer decides whether to extend exception or runtimeException, he / she will consider whether it is reasonable to force the client to declare throws clause / catch exception In some cases, exceptions may be caused by programming errors and "normal operation" errors In this case, it is impolite to force the throw / capture client, and it is more appropriate to make it a runtimeException In client code, an exception is actually used to indicate a "normal operation" error, and it is still suitable to catch it
Related issues (but mainly closed based on Opinions): stackoverflow com/questions/24344511/why-is-catching-a-runtimeexception-not-considered-a-good-programming-practice