3

How can I modify the following code block so it does not rely on try/catch block and IOTimeout. This function gets called every 1 minute. so I just want to process the list of Id's that got queued after the last call to this.

If I just received a null when no messages were waiting in the queue, that would've solved my issue but the API http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.receive.aspx shows all the signatures of receive are synchronous (I can not block the thread) or with a MessageQueueException exceptions which I'm having to tap into by try/catch block. Which I wanted to avoid after reading some article suggesting not to use try/catch to control program flow.

    #pragma warning disable
    public void ConsumeEvents2()
    {
        var listOfJobs = new List<int>();

        try {
            while (true) { 
                // receive all messages and add in a list to process later
                Message message = messageQueue.Receive(new TimeSpan(0,0,3));
                JobEvent evt = (JobEvent)message.Body;
                listOfJobs.Add(evt.DataNumber);
            }
        } catch (MessageQueueException e) {
            if (e.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout) {
                // process the numbers received
                ProcessJobList(listOfJobs);
                return;
            }
            throw;
        }

    }
    #pragma warning restore
P H
  • 963
  • 1
  • 7
  • 8
  • this is a web app, but it doesn't matter. you are still catching the exception at application level – P H Sep 15 '11 at 02:56

2 Answers2

1

try not accessing it directly, take a look at this code from microsoft-

using System;
using System.Windows.Controls;
using System.Windows.Messaging;

namespace ReceivingApplication
{
  public partial class Receiver : UserControl
  {
    public Receiver()
    {
        InitializeComponent();

        LocalMessageReceiver messageReceiver =
            new LocalMessageReceiver("receiver",
            ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);
        messageReceiver.MessageReceived += messageReceiver_MessageReceived;
        try
        {
            messageReceiver.Listen();
        }
        catch (ListenFailedException)
        {
            output.Text = "Cannot receive messages." + Environment.NewLine +
                "There is already a receiver with the name 'receiver'.";
        }
    }

    private void messageReceiver_MessageReceived(
        object sender, MessageReceivedEventArgs e)
    {
        e.Response = "response to " + e.Message;
        output.Text =
            "Message: " + e.Message + Environment.NewLine +
            "NameScope: " + e.NameScope + Environment.NewLine +
            "ReceiverName: " + e.ReceiverName + Environment.NewLine +
            "SenderDomain: " + e.SenderDomain + Environment.NewLine +
            "Response: " + e.Response;
    }
  }
}
ghostbust555
  • 2,040
  • 16
  • 29
0

In Windows Forms applications, when an exception is thrown anywhere in the application (on the main thread or during asynchronous calls), you can catch it by registering for the ThreadException event on the Application. In this way you can get rid of adding try/catch for all the methods.

Application.ThreadException += new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod)

private static void MyCommonExceptionHandlingMethod(object sender, ThreadExceptionEventArgs t)
{
    //Exception handling...
}

Application.ThreadException Event

For ASP.NET Applications

To create a global handler in a page, create a handler for the System.Web.UI.TemplateControl.Error event. To create an application-wide error handler, in the Global.asax file, add code to the System.Web.HttpApplication.Error event. These methods are called if an unhandled exception occurs anywhere in your page or application, respectively. You can get information about the most recent error from the GetLastError method.

Below are some interesting threads for you.

.NET - What's the best way to implement a “catch all exceptions handler”

Why global.asax Application_Error method does not catch exceptions thrown by ASMX service?

global.asax Application_Error not firing

Community
  • 1
  • 1
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • What about other exceptions that can be thrown in other parts of the code? Wouldn't this swallow all other exceptions? Is there a way to rethrow the exception from the handler so that it appears thrown from the original place? – Ryan Stecker Sep 15 '11 at 00:56
  • @ Ryan S : This method catch all the exceptions. You can identify exception type (Eg: ListenFailedException) from here and handle appropriately from a single place. Yes, re-thrown exceptions are appears thrown from the original place. – CharithJ Sep 15 '11 at 01:19