Exception handling: if the condition is against the exception

To verify that the parameter is correct, C uses a bunch of IF Else conditions to determine if this is true. In JAVA, he has an Exception facility to handle this. What use of these two methods, what are the pros and cons of these two methods? Thanks!

Hello!

0
source share
1 answer

Personally, I like the assert statement.

But in this context, I prefer Try-Catch for complex problems and if-else for simpler problems.

If-else (s) are not readable and supported where there are many, especially for a complex problem. That is why the attempt of salvation comes to the rescue. Assuming this input is correct, let's process it. When this entry turned out to be incorrect on some lines, just go to the catch block and return the default value.

He argued that the try-catch attempt is slower. Well that's true. But how much slower? 0.1 ms? 1 ms? I will sacrifice these milliseconds for readability and maintainability.

After all, there are prerequisites for programming in every function / procedure. We all know what the precondition means. The best way (my personal opinion) is to claim that all parameters meet all the prerequisites. Thus, we can assume that this entry is always correct, since the assistants on the other floor already guarantee this.

+1
source

All Articles