First of all, run it under the debugger and find out where the exception was thrown. Put a breakpoint before it happens and use the VS "Watch" window to inspect variable. Consult the device documentation on the parameters required for the call in question and other requirements. Show VS "Call stack" windows (under the "Debug" menu) to find out the stack and the path of the propagation of the exception. You will see all what happens starting from the root cause of it, from the initial UI event, for example. Fix the problem.
You need to catch all exceptions on the top stack frame of each stack. Don't catch too much, keep it to necessary minimum, where you really know what to do with exception handling. UI has a special feature to catch all exception in the top even handling loop. For WPF, this is done via the
Application.DispatcherUnhandledException
event:
http://msdn.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx[
^].
You should always do that. In case of some problem, you can alternatively add additional code for the research: when some exception is caught, output its stack trace:
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[
^].
This is just a string, and it will show you the same exception propagation path, as with the "Call Stack" used under the debugger. In rare cases when debuggers is hard to use, you can simply log exception information (with stack trace) and still dig out the problem. For logging, please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[
^].
—SA