Recently I'm refactoring a .net program which will use a custom usb device. The device comes with a dll for communication. The dll is written in c and by checking the header, it defines a set of error return codes.
The first step to comminicate with the device is to open the device.
In dll, the open function looks like this:
// return different codes, such as OK, ERROR_CANNOT_CLAIM_INTERFACE, etc.
int DLL_Open();
In the .net program, it's using exception for error code:
// Return true if succeed. Throw exception if there is error.
bool Open()
{
int flag = DLL_Open();
if(flag == OK) return true;
else
{
if(flag == ERROR_CANNOT_CLAIM_INTERFACE)
throw USBException();
// ...
}
}
My question is, why to use exception instead of simply using error codes as the dll API? I read some articles mentioned "Don't use exception to control application flow", and I think the exception here is kind like controlling the flow.