Implementing a global exception handler in XNA for WP7 using XNA
I have a WP7 game that has symptoms similar to the following post:
WP7 App exits when deployed
It works fine when run through the debugger on the device, but when deployed it does not run. Also when I unplug the data connection while debugging, it runs for another 30 seconds or so and then exits!
So, I am trying to somehow trap the mysterious exception that I as开发者_JAVA技巧sume must be thrown at some point.
Question 1 - Where can I put a try catch block so I can trap the error? Currently I have a single class derived from Game which gets executed automatically by the framework.
Question 2 - What is the easiest way to get the error message of the exception? I am using XNA so I assume a dialog box is out of the question.
Thanks! swine
Q 1.
There doesn't appear to be a way to add a global exception handler to a WP7 XNA app.
Where to add appropriate error handling will be dependent upon your code. As you haven't shown any code or even described what the app/game does it's hard to say. If you're using the data connectioion I'd look at the code you are using to make a network/web call. I would guess that you're not handling a connection dropping and you currently have a 30 second timeout on the request. Hence the applicaiton crashing 30 seconds after you disconnect from the network.
Q 2.
You can show a MessageBox from XNA. See http://msdn.microsoft.com/en-us/library/ff827868.aspx
You should be able to use a similar technique that we use on the Xbox 360 for Xbox LIVE Indie games. Nick Gravely shared the original sample here
http://blog.nickgravelyn.com/2009/07/a-more-robust-exception-system/
and then I enhanced his to do a little more here.
http://www.xnadevelopment.com/tutorials/tellmewhatswrong/tellmewhatswrong.shtml
The same technique should work on the phone.
精彩评论