6 ms·
I actually like it. I also create my own exceptions for rare (exceptional) cases, just not to forget to handle them. But some Java SDK exceptions drive me craz
by deepsun 7y ago
I actually like it. I also create my own exceptions for rare (exceptional) cases, just not to forget to handle them.
But some Java SDK exceptions drive me crazy. For example `URL.parse("http://example.com")` http://example.com")` over hard-coded strings that I know 100% won't throw exceptions, but I still to catch 3 of them.
- vips7L 7y ago`URI.create("https://example.com")` https://example.com")`
- twic 7y agoThat's why URI has a factory method that you're only supposed to use for known strings (although of course this can't be checked): https://docs.oracle.com/javase/7/docs/api/java/net/URI.html#create(java.lang.String) https://docs.oracle.com/javase/7/docs/api/java/net/URI.html#... After URI appeared in 1.4, the only reason to use URL was to create a URLConnection from a URI. Since openConnection() throws IOException, it's not a big deal that toURL() throws a MalformedURLException - just catch it along with all the other IOExceptions. Since 11, there's no reason to use URL at all, because you can use HttpClient to actually do HTTP.
- meddlepal 7y agoOk I'm a veteran Java developer that understands exactly what you're saying but try explaining that to people unfamiliar with Java or junior engineers and watch their brains glaze over. The JDK is filled with a ton of dumb "once upon a time we thought this was okay..." things that don't properly encode the correct modern idiom
- xenomachina 7y agoFor a long time I've wanted a sort of "local deprecation" tool where we could have a list of things in the JDK that shouldn't be used, and any direct reference to them would cause our build to fail.
- erik_seaberg 7y agohttps://errorprone.info/ https://errorprone.info/ can be configured to look at the AST and warn or reject a lot of specific problems.
- cesarb 7y agoWe use https://github.com/policeman-tools/forbidden-apis https://github.com/policeman-tools/forbidden-apis for that.
- twic 7y agoYes, it absolutely is! But then so are all other languages of its vintage. Part of learning a programming language is learning that. Newer languages definitely have an advantage here, in that they haven't been around long enough for people to have figured out which bits of them are dumb.
- ivolimmen 7y agoIt's worse. In 1.4 they added URI because URL is very flawed. You should never use URL. The equals method actually does a DNS resolve and compares the ip addresses. This means that comparing two URL's on the same server will always return true when compared. A lot of URL's you compare will give you true as there are a lot of sites run on the same machine with the same ip address.
- sebcat 7y agoHad this happen in production in a third party library we used many moons ago. Sysop came and asked us why we did six figure DNS lookups over a short period of time (24h? Less? Don't remember). Would probably have gone unoticed at most AWS/GCP/Azure shops today.
- swish_bob 7y agoWorse was when I had somebody adding the ip address as an Inet4Address on every message passed between machines in a production environment that explicitly didn't have DNS (banks have occasionally very odd ideas about securing subnets). Every single message was doing a reverse DNS lookup and then timing out. And there were a _lot_ of messages.
- franga2000 7y agoWait, seriously? Did whoever designed that have any idea what URLs actually were? Even without DNS, comparing the hosts is no way to compare URLs. I can't think of a single example of two 'equal' URLs whose strings don't match (up until the parameters, at least)...
- matthewmacleod 7y agoYes - bear in mind the URL class has been around since 1995, when the landscape was somewhat different! At that point, URLs resolving to the same IP were considered equal, even if the host names were different. Even now, there’s no real difference between say “http://example.com” http://example.xn--com-9o0a and “http://example.com:80”; http://example.com:80”; it would have been reasonable to consider these equal.