Re: How do you exception in your daily C++ programming?
bingfeng wrote:
We are all konw traditional "return-value" or global errno varibale
management policy is error prone, but I have not seen using exception
thoroughly in an industrial applicaion. Most of my time is playing on
windows and I always found use exception which wraps GetLastError()
call automatically makes sense to me. It significent simplify the
program logics.
I fully agree with you here. However, the use of exceptions also requires
exception-safe code, in particular concerning resource management.
However, I met following issues:
1. I log the error to file. what we can get from std::exception (I
derived my windows exception classes from it) is just an error
message. that's not enough since I don't know where the exception
throw! In order to make it available, I use a MACRO to collects the
file name and line number. I hate macro indeed, but do I have anothr
way?
There is no other way than by using macros, except perhaps a
platform-specific way similar to a debugger's. However, this has nothing to
do with the use of exceptions.
2. just do global catch in program entry (the main() function) is
clearly not enough. I use multithread always and I provide global
catch for every thread entry so that an exception will not affect
other running threads. Any ideas for that?
I do so, too. The advantage is that you can log this easily. The
disadvantage is that this doesn't actually handle errors. A thread that is
terminated and that another thread is perhaps waiting for is just a PITA.
Further, a "catch(...)" under MS Windows might also catch non-C++
exceptions like access violations, where you would often prefer a clean
shutdown of the application.
3. we have some teams do different components respectively. Maybe
others do not like exception, maybe others do not use catch at all. In
order to avoid program termination from unhandled exception. I had to
catch(...) for each public interface and return an error code if
necessary. Any good practice here?
If your code has a consistent exception policy and the interface has a
consistent error-code policy, the try-catch-clause here can probably be
implemented using a macro, because it is the same code over and over.
AFAIK, using exception is not welcomed in some teams. I raise this
question since I want to collect good practices and argue that we can
use exception without exception. Indeed, I found some apps claim that
they need not use exception do few error management.
Actually, some applications are so simple that simply outputting an error
message and calling exit() is sufficient. Other code might have to provide
a C interface and thus doesn't want to use exceptions in the first place.
Other than that, you still have C++ programmers that haven't fully grasped
the idea of RAII, which is a basic building block for exception-safe code.
From those people, you often hear that use of exceptions is bad, slow,
error-prone and causes resource leaks.
In any case, using exceptions is an all-or-nothing decision. You can not
convert something to use exceptions without having to change all code that
uses it and that even though the interface seems to not have changed.
Uli
--
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]