Sign in to follow this  
rpg_code_master

Flag Model over Exception Model

Recommended Posts

Hello I am in a bit of a dilemma. I have a messaging object that has a send method. I am in a few minds as to how to handle this. One is to return in a HRESULT style, like MessageSendResults.Success, .MissingRecipient, .CannotConnectToHost etc... Another is to throw exceptions. The problem I see with this, is that if there is more than one error, you cannot handle them all at once. Am I just being pedantic about this? The third is to return a struct with lots of info in it, such as recipient specific results along with reporting information. I have my ideas of what model I would like, but I would like to see some other ideas and opinions also. [code lang="cpp"] <return value here> MyMessagingObject.Send( <params here> ); [/code] Thank you very much for your time, Richard Hughes Thank you for your time.

Share this post


Link to post
Share on other sites
Quote:
Original post by rpg_code_master
Hello

I am in a bit of a dilemma.


Unfortunately, you do not state the dilemma.

You list two options, exceptions and return codes (all known and interchangeable solutions), yet not explain the problem.

For example, if you're using a certain compiler, there might be problem with implementation of exceptions. Or your API might require too many checks if used with return codes.

But nowhere do you list reasons for your dilemma. You merely have two solutions, looking for a problem.

Quote:
Another is to throw exceptions. The problem I see with this, is that if there is more than one error, you cannot handle them all at once. Am I just being pedantic about this?


An error either is or isn't. You cannot have two errors at same time without quantum computing. With sequential execution, only one thing can happen at the same time, which either is or isn't fatal.

Quote:
The third is to return a struct with lots of info in it, such as recipient specific results along with reporting information


This is what exceptions are, usually they contain additional information.



There's also another way of handling errors, something that your title implied, but fortunately wasn't the case: GetLastError() style. That one is just horrible for most purposes, since it implies errors will not be checked by default.


But again - there is a finite number of ways one can handle errors. What you now need to do is analyze the problem you have, and then find optimal solution.

Share this post


Link to post
Share on other sites
Quote:
rpg_code_master
The problem I see with this, is that if there is more than one error, you cannot handle them all at once

Agreeing with Antheus above, you can't really have multiple errors. If something goes wrong - that's it; from herein the state of your program is not how it is supposed to be (hence the error). So just throw the first exception.

Quote:
The third is to return a struct with lots of info in it, such as recipient specific results along with reporting information.

Throw a class.

class myException
{
std::string m_msg;
int m_errorCode;
std::string m_window;

myException(std::string m, int c, std::string w){m_msg = m; m_errorCode = c; m_window = w;}
}

...

void foo()
{
if(...something fails)
throw myException("unkown failure", -999, "my_fubar_window");
}
...


try
{
foo();

}
catch(myException& e)
{
std::cout<< "error " << e.m_msg << " @ " << e.m_window << " code:" << m_errorCode << std::endl; //or stderr, or wherever
}



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this