3 ms·
Exceptions in C++ are rotten. void read_file_by_lines(const char* path, void (*callback)(const char* line)) { FILE* file = fopen(path, "r");
by gakada 10y ago
Exceptions in C++ are rotten.
void read_file_by_lines(const char* path, void (*callback)(const char* line)) {
FILE* file = fopen(path, "r");
size_t length = 0;
char* line = NULL;
while (-1 != getline(&line, &length, file)) {
callback(line);
}
free(line);
fclose(file);
}
This C function should be compatible with C++. If exceptions are off, it is. If exceptions are on then it's dangerous because if an exception is thrown through callback, then the file handle and line buffer will leak.
C++ exceptions are predicated on the assumption that all resources are managed using RAII. In the real world, this isn't true, especially considering that C doesn't even have RAII.
Python programmers (including me) love their exceptions, because Python is a dynamic language, and exposes system resources as reference counted objects.
- pjmlp 10y agoThis is the real issue, so many C++ features are hampered due to the drop-in compatibility with C code.
- pif 10y agoYour example is wrong. This function is compatible with C++ as soon as you document it as not accepting exception-throwing callbacks.
- gakada 10y agoI'm sorry, I can't capture such a complicated issue with a pithy example. You will have to settle for my illustrative one. The most general definition of the restriction is that C++ code cannot throw exceptions through exception unsafe code. How do you know if your C++ function was called from exception unsafe code? You can't! So it's only ever safe to throw an exception when you are expecting it to be caught immediately i.e. you can use exceptions as glorified error codes, but not much else. In Django I can throw a Http404 exception from deep inside a view and the server will come up with a 404 response. I can assert and the client will get a 500 error but the server won't go down. This is the real power of exceptions, and God help anybody who tries to do it in C++.
- pjmlp 10y agoThat is exactly one of the pet peeves I have with C++ and although I like the language I am happy to earn my money using other languages. In the enterprise context you can hardly count on proper documentation, specially in projects with high attrition of consultants where each one comes, implements something and departures to the next gig. Not to count the commercial libraries distributed in binary only form. So one never knows if the code is exception safe, don't work with exceptions or is exception happy.
- wyldfire 10y agoYour example strikes me as "exceptions in C++ are [yet another] high-discipline task to manage." I think there are examples related to inter-library exceptions that might be much more subtle than this one, but I've never understood the subtlety.
- perspectivep 10y agoThe solution is to just make RAII wrappers for that stuff.
- coherentpony 10y agoIs the fault the exceptions or is it that the person that wrote this C++ function didn't use objects to manage their resources?