Question

I started using exceptions some weeks ago and now I wonder if there is a way to just throw a warning. This warning shouldn't force the application to exit if it isn't caught. I will give you an example in what situation I would like to use that.

There is a system that appends properties to unique ids. When I somehow try to add a property to a not yet existing id, the system should create that id internally for me, add the property to it afterwards and return the result. Of course this can't be done quietly. But since the application could stay running, I do not want to throw an exception.

How can I notify that something wasn't quite correct, but the system runs on?

Was it helpful?

Solution

Who do you want to notify? The end-user? In which case, just write a suitable message to cerr. Or better, write a wrapper function (e.g. LOG_WARNING()) to do it in a controlled manner. Or better still, use a logging framework.


But since the application could stay running, I do not want to throw an exception.

Note that an exception doesn't have to result in the application terminating. You can catch an exception higher up the stack, and handle the situation appropriately.

OTHER TIPS

No, that's not possible. You can only throw and catch exceptions. If you want to be cheeky you could do

class warning : public std::exception
{
public:
     warning(const std::string& msg) {}
     const char* what() { return msg.c_str(); } //message of warning
private:
     std::string msg;
};

Then you can:

throw warning("this is a warning");

This could be an artificially made up warning system if you want.

While there's no throwing a warning. I believe the functionality you're looking for is available from errno

You can set it to any of the standard errors or make up your own error codes. (Please document them well though.)

This could be useful if your library is meant to be used by other developers. An example of when this might be useful is with a JSON parser. JSON supports arbitrarily large numbers with arbitrary accuracy. So if internally your parser uses doubles to represent numbers if it encountered a number that it couldn't represent then it could round the number to the nearest representable number the set errno=EDOM; (argument out of range) that way, it leaves the decision up to the developers as to whether the rounding will matter. If you want to be super nice you could even add in a way to retrieve the locations of the rounds possibly even with the original text.

All of that said, this should only be used in situations where:

  • the warning really can be bypassed completely in some scenarios
  • the root source of the warning is input to the library you're writing
  • the in some situations the consumer of the library might care about the warning but most of the time wouldn't.
  • there's not a more suitable way to return the information (like a status passed by reference with an overload that doesn't require the status)

Just print a message to stderr or to your logs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top