3 ms·
In Java, that would take a handful of code, a try/catch block, a loop, some sort of buffer and would ultimately create much more verbose, much less readable cod
by kenjackson 16y ago
In Java, that would take a handful of code, a try/catch block, a loop, some sort of buffer and would ultimately create much more verbose, much less readable code.
How do you get away with none of this in Python? 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.
And while you don't necessarily need a for-loop to process the data, you often will.
- bad_user 16y agoYou 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.
- rmc 16y ago> How do you get away with none of this in Python? It's optional. You don't have to catch any exceptions (unlike java), however if you want to you can catch, separately, any one of the dozen exceptions that could be raised (IOError, permission error, memory error etc.). One reason I love python is that the common use case is easy. 90% of times people read from files they don't need to handle every exception there and then, ergo python makes that possible. This makes python much more enjoyable, productive and easy to use.
- Goladus 16y agoExceptions in Python are automatically runtime errors and will stop execution with a useful error message if they aren't caught. Is this not true in Java? In Python, you don't need to add a try/catch block unless you specifically need to do something and continue execution after an exception.