2

I am writing a program that retrieves data from an industrial device and exports it to a MySQL database. For interacting with the device, I am using a DLL driver provided by the manufacturer. In this DLL, there is a custom-made exception which is thrown when there is a communication error or any other type of abnormal situation when communicating with the device.

The problem is that there is a single class exception. So, when I catch an exception of that type, I cannot really tell which particular problem happened since there are several ones (connection timeout, trying to access an invalid memory address, etc.).

So what I am doing for the moment is to use the Exception.Message property. So for example if the exception message contains "Invalid Address" then I know that the error is due to trying to access an illegal address within the device, similarly if the exception message contains "connection timeout" I know that there was an exception timeout. But this approach looks really dangerous since the messages may change in future releases of the DLL.

What can I do to deal with this problem in a better way?

1
  • This is very much by design. These kind of exceptions can't be handled, you need a human to solve the problem. One that fixes the connection string, configure the dbase engine or database, make changes in your code, etcetera. All you can reasonably do is display the exception message so the guy knows how to fix the problem. Never try to catch and handle exceptions that you cannot fix, always give as much information as possible. Commented Nov 20, 2013 at 14:03

1 Answer 1

1

If you're lucky the "generic" Exception has an InnerException with the real exception type.

If all of the exceptions have the same type and the only difference are the messages, then your solution is the best. Just hope the manufacturer does not change anything and make sure to test every new version...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Willem for your quick reply...I checked the InnerException and all the exceptions have the same type, confirmed. So, I will keep using my solution based on looking at the messages. I don't really like it but I cannot see what else to do (apart from contacting the driver's vendor and ask to introduce exception subclasses).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.