I know that you shouldn't use exceptions for non-exceptional conditions, but I wanted to run this past others to see if what I'd like to do is really that bad.
I have a routine that is attempting to get a message off of a MSMQ message queue. If no messages is available on the queue, it checks a secondary source to see if a message is available there. An example of the routine follows:
void CheckForMessages(out Message msg, TimeSpan timeout)
{
try
{
queue.Peek(timeout);
}
catch(MessageQueueException ex)
{
if (e.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
{
if (messageAvailable)
{
msg = SecondaryMessageSource();
}
}
throw;
}
msg = queue.Receive();
}
MSMQ provides no mechanism for checking the count of messages on the queue. The only way to determine if a message is available is by using Peek. If a MessageQueueException occurs with its error set to IOTimeout, then the Peek has timed out and no message is available on the queue.
This routine will be called within a loop on a thread whose sole purpose is to retrieve messages. The timeout will be in the millisecond range. The call stack consists of a single method, and the thread is responsible for doing nothing else. I know that the method will be constantly throwing exceptions, which is considered bad practice, but in this case is it really all that bad? If so, does anyone have any suggestions on how to accomplish this task without the code becoming totally convoluted?
Peek?