0

I have an old C based project, which I would like to port from an Atmel processor to Raspberry Pi.

At the time that it was written, C++ was not an option, and it would be too much effort, almost a rewrite, to convert it all to C++.

Some problems/crashes can't be (easily) caught by C, so sometimes my program will just die & I would like to send a last chance cry for help before expiring. No attempt at recovery and I can even live without details of the error, just so long as I get a message telling me to visit the equipment

Long story short, I think that I could have better error detection if I had exception handling.

I am thinking of using exception handling as chance of alerting me to go to the device and fetch the complete error log, reset the hardware etc. C won't always give me that last gasp chance to do something, if my code goes bang

Since I don't want to do a total C++ rewrite, would it be enough just to wrap main() in try / catch?

Is that technically enough, or do I need to do more?

Other than more detailed error reporting, is there anything to gain by wrapping every (major) function in it's own try / catch?

3
  • 2
    Exceptions are thrown by a throw statement. Seg faults aren't required to throw exceptions; they are a symptom of undefined behavior. Commented Nov 15, 2016 at 21:20
  • 1
    Segmentation faults are not guaranteed to be caught by a try/catch(...). In fact you should not continue after a segmentation fault even if you have specified a C-style handler. I suggest you just add try/catch around each C function with extensive logging and hope for the best! Commented Nov 15, 2016 at 22:03
  • Please forget that I ever said seg fault (but +1, both); it was a bad example & I will remove it from the question (I check for NULL parameters, but may not always check for NULL before every dereference). But, @PeteBecker, exceptions do not only arise from throw, and that is what I am trying to catch Commented Nov 15, 2016 at 22:41

1 Answer 1

1

Other than more detailed error reporting, is there anything to gain by wrapping every (major) function in it's own try / catch?

Firstly, only catch exceptions where you are in a position to alter the behaviour of the program in response to them (unless you're simply looking to add more contextual information via std::throw_with_nested())

Secondly, a c program will not exhibit RAII, so throwing exceptions in this circumstance is likely to leak resources unless you wrap all your handle and memory allocation in smart pointers or RAII-enabled handle classes.

You should do that before you consider adding exception handling.

If the program is likely to be actively maintained into the future, there is probably mileage in doing this. If not, probably better to leave sleeping dogs lie.

Sign up to request clarification or add additional context in comments.

3 Comments

Some good points there (+1), but, as I said, I just want a last gasp chance to send an HTPP or TCP message, or maybe SMS Or email. Basically, not just die, but give a last gasp, then die
Your best bet is probably a signal handler (on unix-like systems) or structured exception handling on windows.
Wow! Signal handler is good! How did I miss that? Just finished adding signal handling for that very reason to C doe at work. Feel free to post that as an (other) answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.