4 ms·
You need a try/catch block to catch exceptions on open and read don't you? There's no guarantee the file exists, or you have permission, or enough
by bad_user 16y ago
You need a try/catch block to catch exceptions on open and read don't
you? There's no guarantee the file exists, or you have permission, or
enough memory to read the file.
Doing a try / catch implies that you can recover from it.
That isn't the case most of the times (you cannot get extra permissions to a file, you cannot recover from not having enough memory, and if the file is missing that's a bug).
What usually happens in both Java and Python is that you have a top-level try/catch that catches everything and just does logging.
The difference is that in Java you catch and re-throw the exception, or put the exception type in the method's interface, which IMHO leaks encapsulation. Or just turn everything into RuntimeExceptions with a little compiler-plugin magic.
Either way, checked exceptions are a big fail.
- kenjackson 16y agoDoing a try/catch doesn't imply anything, except that an exception occurred that you caught. Behavior after that is completely app dependent. Top-level try/catch blocks that simply do logging and nothing below that may not always be a bad idea, but its usually a sign of a poorly designed app. And why can't you get extra-permissions or recover from not having enough memory? I've certainly worked on systems with the later, where we catch an OutOfMemoryException and then do some stuff we're required to do -- including in some situations to get memory back (which is for example, why we'll often pre-JIT and keep loaded code required to free up memory). Checked exceptions are a big fail, but largely because they're a broken way of thinking about exceptions (and why Java was the last major language to have them). EDIT: I glossed over the fact that you may have been actually getting at which is the requirement associated with checked exceptions. You're absolutely correct there. No argument from me. I wasn't think of that originally, as I'm not a Java dev.
- rbanffy 16y ago> Doing a try/catch Doing try/catch tells the compiler you know what you are doing. I prefer compilers that take that as a given.