Rpgccv Rpgccv - 1 year ago 85
C# Question

UWP . How to prevent app from closing on error?

I have an app that give the user the possibility to pick a file, from the pc/mobile. If the file is corrupt or is not well formatted the app crashes and close itself!!
Since that file become the default one, the user have to uninstall and reinstall the app so everything get the original setup.

This is an example, the real question is how to prevent any app from closing in any type of error!?

Answer Source

In those cases exceptions are most likely being thrown. When an exception is thrown, it has the opportunity to be caught using try and catch.

     //Do something
     //Something went wrong.

It's up to the developer to decide what to do when faced with an exception. Ignoring it (leaving the catch block) is usually a bad idea. Instead, you may want to do something like show a message to the user and ask them to pick a different photo.

how to prevent any app from closing in any type of error!?

Apps usually cannot do this. What should the application do when it is faced with an error, and the rest of the code cannot work when that error happens?

It's up to you to decide how an error should be handled, and how to safely continue. Putting your entire application in a try/catch doesn't work as the app will not be able to do anything meaningful from then on out.

If there is an area of your application that needs error handling, then add it. Sometimes there is no good way to continue running a program, and closing it is the best option.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download