1

I want to circunscribe this question to the specific context of WPF aplications. The accepted answer says

"You should only to catch exceptions that you can actually do something about"

and also

"Note that if you're simply trying to catch any unhandled exceptions that might occur for the purposes of logging or error reporting, you should be using the AppDomain.UnhandledException event"

Well, all would be good and well EXCEPT (pun intended) that I had a very serious problem while deploying a WPF application which would crash right at application startup, with the dreaded IOException error in PresentationFramework.

I tried the Application.Current.DispatcherUnhandledException, but the crash apparently happended outside its grasp. I tried Windbg but the error messages were still elusive.

Then I followed advice from this post (changing app.xaml Build Action property from "Application Definition" to "Page" and putting Main() function inside App.xaml.cs itself), putting a try/catch with a MessageBox (it could be a logging call, whatever), and the message displayed immediately led me to the solution.

So the question is:

Considering WPF has its own esoterical bugs, is there any actual problem in putting Main() function body inside a try/catch?

Here is my current code:

public partial class App : System.Windows.Application {

    /// <summary>
    /// Application Entry Point.
    /// </summary>
    [System.STAThreadAttribute()]
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public static void Main() {
        try {
            var v3d = new App();
            v3d.InitializeComponent();
            var shellview = new ShellView();
            v3d.Run(shellview);
        } catch (Exception e) {
            MessageBox.Show(e.ToString());
        }
    }

And for the record, I was getting "Cannot locate resource 'app.xaml'" caused by some culture mismatch, the problem happened in only one of two similar machines apparently because one OS (Win7) is English and other is Portuguese. I solved it with this answer.

Community
  • 1
  • 1
heltonbiker
  • 26,657
  • 28
  • 137
  • 252

0 Answers0