Whillst developing our app we are sometimes mistakenly passing a handle for a window that cannot be captured through to CreateCaptureItemForWindow (eg a child window).
I am trying to catch and debug such errors, but the catch never works, instead I get an exception.
This is my current code and the exception:

We hope to eliminate all such errors but if it happens in future we would like to catch rather than crash.
How can I do that?
I'm using VS2022 under Windows 11 V23H2