Runtime Handling Exceptions in certain circumstances valid?

As I understand from several tutorials, RuntimeExceptions are not really supposed to be caught, because they should reveal a mismatch in the use of methods, especially the API, right?
In addition, it can be assumed that the program cannot recover from RuntimeExceptions.
Now I have a case where I can get an IndexOutOfBoundsException due to incorrect user input: the program receives a string from an HTML form and wants to check if it contains an integer.
Basically, the assigned string is mapped to a pattern, and then the position of the comma is determined. Subsequently, you need to check the first digit after the comma to decide how to round it correctly. This is the critical part:

firstDecimalChar = formattedValue.charAt(dotPosition + 1); 

This operator throws an exception if the user entered something like "27." Due to the fact that it is based on a strange user input, I wondered if this exception really behaves like a regular exception (so to speak).
The reason I think this is in most tutorials (e.g. oracle ) is that they classify the marked Exceptions as errors that are the result of incorrect input, i.e. The wrong path is the name of the file to be opened, and with some correction, or the user will tell you that you can restore this error. Same thing: you can simply set the above variable to zero, because that means that when someone inserts "27.", → "27.0"

Is this a gimmick and actually indicates that the requirement is strict? Of course, you can check in advance if the position is not outside the range, but my question is whether there is such a straight line between checked and unchecked exceptions. I am wondering if there are any technical flaws, why exceptions should be avoided as often as possible. (Someone mentioned JVM overhead, but how significant is that?)

It seems to me that I need to get a deeper explanation, because most textbooks do not convince me: "you simply should not do this." Since I think that in this case this exception behaves like a checked exception from which it is easy to recover.

Note: In my code, I check whether the line position is valid or not, and not catch an exception - so this is not part of this question :) But this case keeps me busy, so far.

+6
source share
4 answers

Is this a ruse and does the demand really indicate that is strict?

No, I think that in certain situations it can be excluded by uncontrolled exceptions ( RuntimeExceptions ).

For example, if the user enters a number, I think it's fine, what needs to be done

 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, do not bother to catch it (or catch it at the highest level, for example, in handleRequest or something else, and, for example, return an error of 500), On the other hand, if you can provide for the generated exception (and it’s problematic to use regular control structures such as if to cover these cases, for example, in the above example NumberFormatException ), catch it and handle it.

As it happens, RuntimeExceptions often correspond to programmer errors and say that "do not catch RuntimeExceptions" can probably be considered as a good approximation of what can be caught and not caught.

When a developer decides whether to RuntimeException Exception or a RuntimeException , he / she will think about whether it is wise to force clients to throw throws or throw an exception. In some cases, the exception may be due to a programming error or a “normal operation” error. In such cases, it is dishonest to force throws / catch clients, and it is more suitable to make it a RuntimeException . In client code, where an exception is actually used to indicate a "normal operation" error, it is still appropriate to catch it.

A related question (but closed mostly based on opinion): stackoverflow.com/questions/24344511/why-is-catching-a-runtimeexception-not-considered-a-good-programming-practice

+6
source

As I understand from several tutorials, RuntimeExceptions should not actually be caught, because they are inadequate use of methods, especially the API, right?

Not really. You can always catch a RuntimeException and wrap it in marked Exception and throw back to the caller to decide whether to kill the application or complete the error in a format that the user can understand

Example:

 try { //read parameters String processType = args[9]; String username = args[11]; //read more parameters.. } catch (ArrayIndexOutOfBoundsException e) { throw new InvalidInputException("Wrong number of input parameters passed. See usage notes"+e); } 
+2
source

I think a Runtime exception means something that should never happen at runtime. For example, an IOException exception is no exception: an IOException exception occurs because something outside the control of the program (the file system in this case) is incorrect.

However, an ArrayIndexOutOfBoundsException exception occurs when the program is fully monitored. So this is a RuntimeException. This means that the programmer did something wrong. For example, an incorrect calculation of the array index.

Let me depict a program that does not interact with the outside world at all (without I / O, etc.), this program should never throw an exception, because it completely controls everything that happens. If he throws an exception, it means that the programmer did something wrong (or was very lazy).

Note. I completely rewrote this answer, because at first I did not see a note at the end of the question :)

+1
source

Some execution methods or third-party methods (for example, Integer.parseInt ) report a validation error by Integer.parseInt exception. If we need this check function, we need to catch an exception.

It would probably be better to have something like Integer.isValid(String x) , but plentiful replacements of standard libraries are too common.

+1
source

Source: https://habr.com/ru/post/988769/


All Articles