salvalcantara
salvalcantara

Reputation: 480

Dealing with exceptions in VB.NET in terms of Exception.Message

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?

Upvotes: 2

Views: 168

Answers (1)

Willem
Willem

Reputation: 5404

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...

Upvotes: 1

Related Questions