67 karma · joined Aug 19, 2017 (9y ago) · 49 submissions
comment 9y ago · link · parent
The OS/process analogy doesn't hold here. The process has completely isolated state from the kernel.
comment 9y ago · link · parent
comment 9y ago · link · parent
It doesn't matter if throwing ValueError exceptions on sqrt(-1) is well-defined, continuing to run the program by ignoring the exception is no less harmful than silent integer overflows or buffer overruns.
I don't restart my OS when a process crashes because it has been designed to use hardware mechanisms to clean up dead processes. I absolutely do restart my OS when it kernel panics, it doesn't try:except:log:continue.
comment 9y ago · link · parent
comment 9y ago · link · parent
comment 9y ago · link · parent
I'm arguing that an unintentional or unexpected ValueError, IndexError, TypeError, etc. means your program is buggy and there is no sane/practical way to 100% safely recover from it outside of restarting the process.
Your example of the catch-all handler in a modular HTTP server is inappropriate and dangerous. Continuing to run a server with persistent in-process state after an unexpected exception risks corrupting data. Just because Python doesn't have C pointers doesn't mean it can't have inconsistent or incorrect state that can affect other server threads.
comment 9y ago · link · parent
comment 9y ago · link · parent
Python's philosophy of giving well-defined behavior to meaningless code seems wasteful. If your code is unintentionally executing sqrt(-1) (or unintentionally indexing out of bounds, etc) then something is wrong with your program. You don't necessarily need the behavior to be defined if there is a bug in your program. In the case that you want something predictable to happen, better to just abort(). Catching ValueError/IndexError in those cases is futile, how can one trust a buggy program to handle itself?
Python using exceptions to signal both runtime errors and programmer errors is a design smell. The former should always be caught and handled, the latter should never be caught and should probably only reasonably abort().
comment 9y ago · link · parent
If you want to make sure your callers are obeying the interface contract, use assert() to not incur runtime overhead in production and guard against programming error:
int add(int a, int b) {
assert(!__builtin_sadd_overflow(a, b, NULL));
return a + b;
}