Thanks Jason for sharing the link.
I figured out the issue for exception. The program was trying to register the device again when it was already registered so the api call was throwing exception even though the exception details do not tell if the device is already registered.