jsakamoto / ipaddressrange

.NET Class Library for range of IP address, both IPv4 and IPv6.
Mozilla Public License 2.0
368 stars 71 forks source link

`TryParse` static method should catch all types of exception? or not? #24

Open jsakamoto opened 7 years ago

jsakamoto commented 7 years ago

Hi everyone, and contributors of this library:

Few days ago, @zijianhuang said "TryParse static method should not catch all types of exception." with pull request #23 that contains commit:e3a7917.

His comment is:

For the sake of defensive programming and safety, library code should not catch "all of types (any Exceptions)". Catching general exception should be the job of the application and CLR. Major platforms like WinForms, WPF, WCF and MVC etc. have built-in mechanism of catching uncaught exceptions. It is the responsibility of the application developers to utilize those mechanisms built-in.

(Source: Some slight QA fixes in 2 commits #23)

I partially agree his opinion. I always feel it is "foolish" that catching all types without much thought.

But, another point of view, we had an experience that ran into really unexpected type of exception caused by the mistake of my implementation. ( See also #7 and #5 ).

What should I do?

zijianhuang commented 7 years ago

If you post the question to StackOverflow, you will get more in-depth answers. But in fact, you don't need to, since you just need to Google "Catch general exception" you will find many, some of which are from StackOverflow. Also various engineering book like "Code Complete" and "Clean Code" also talk about this.