Class Library Exception Passing To Other Class Library Or Executable?

pointeman
pointeman used Ask the Experts™
on
I want to learn the absolute correct way to pass exceptions from Class Library .dll to other .dll or Executable (Form1). I don't receive the actual .dll exception, instead I receive a general try/catch exception from the Form1 attempt to use the .dll. Help!

[Example Summary]
Click a button on Form1 to clear the local Queue without first specifying the Queue name will cause an exception.
Seems to me the the .dll CreateQueue() exception should 'bubble-up' to Form1.

Does this make sense?

Q1. How do I pass the .dll exception to the .exe (MyForm1)?
Q2. How do I pass the .dll exception to another .dll then to .exe (MyForm1)?

[MyForm1.exe]
public static void ClearQueue()
        {
            msgQueue.QueueName = ""; //intentionally blank for testing purposes
            msgQueue.ClearQueue();
        }


[MyQueueManager.dll] (referenced by MyForm1.exe)
        public static void CreateQueue()
        {
            try
            {
                if (MessageQueue.Exists(_queueName))
                    msgQueue = new MessageQueue(_queueName);
                else
                    msgQueue = MessageQueue.Create(_queueName);
            }
            catch (MessageQueueException ex)
            {
                throw new Exception("MsmqManager.MSMQueue.CreateQueue() Exception: ", ex);
            }
        }

        public static void ClearQueue()
        {
            CreateQueue();

            msgQueue.Purge();
        }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dir Internet Svcs
Commented:
Since your dll is doing a throw, your call to msgQueue will need a try/catch to know the details of the exception being thrown.

Another alternative is to have an Error property within the MessageQueue class and that would have to be set with the error information if an error occurs instead of issuing a throw. Then you will need to test the Error property at various points and act as needed.

Gary Davis

Author

Commented:
Q. Do you think the .dll 'Catch' is correct or do you recommend another way?
Gary DavisDir Internet Svcs

Commented:
The try/catch in the dll looks correct to me.
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Author

Commented:
Q. Do you prefer throw ex; vs my code example?

I've also been working with throw ex.Innexcepion; as well.
Gary DavisDir Internet Svcs

Commented:
I'm not sure I understand. Your example does issue a throw.

Author

Commented:
Thx

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial