HttpURLConnection java.io.FileNotFoundException

You can get a FileNotFoundException from HttpUrlConnection (and OkHttpClient) if your server returns >= HTTPStatus.BAD_REQUEST (400). You should check the status code first to check what stream you need to read. int status = connection.getResponseCode(); if(status >= HttpStatus.SC_BAD_REQUEST) in = connection.getErrorStream(); else in = connection.getInputStream(); HttpStatus deprecated. Latest syntax seems to be: InputStream inputStream; int … Read more

Python’s “open()” throws different errors for “file not found” – how to handle both exceptions?

In 3.3, IOError became an alias for OSError, and FileNotFoundError is a subclass of OSError. So you might try except (OSError, IOError) as e: … This will cast a pretty wide net, and you can’t assume that the exception is “file not found” without inspecting e.errno, but it may cover your use case. PEP 3151 … Read more