
On 11/09/2014 08:11 AM, Roman Cheplyaka wrote:
Let's say you knew what exceptions could be thrown. What would you do differently then?
Typically, if it's a command-line app and something unexpected happens, you simply present the error to the user. That's what exceptions do already without any effort from you.
The output from the exception is usually useless, though. Rather than, *** Exception: EACCES (or something similar, whatever, I'm making it up), I want to show: Insufficient permissions for accessing /run/<app>. This can be fixed by granting write and execute access on that directory to the user under which the application is running. If the operation can fail in different ways -- like if the directory is missing entirely -- I need to pattern match on the exception and display something else. Another example: I don't want to log a "read error" in my daemon, which is what I'll get if I log the exception. I want to know *what* failed to be read. Was it a file (that a cron job deleted) or a network socket (whose connection was dropped)? I need to catch those in the code, where I know what's being read, and what the "read error" means in context.