Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF OpenFileDialog Suppresses Exception
    text
    copied!<p>Perhaps I'm using the <code>OpenFileDialog</code> incorrectly, but I've found that unhandled exceptions are being suppressed whenever the <code>OpenFileDialog</code> is used and the result is passed into my model. </p> <p>Typically I'll hook ino the <code>AppDomain.CurrentDomain.UnhandledException</code> event to handle any unhandled exceptions, but any exceptions being raised after using the <code>OpenFileDialog</code> are swallowed whole.</p> <p>The following is an example to reproduce this behavior. If you run the example, you'll see that the exceptions being thrown in the code behind and the <code>ShellModel.ThrowException</code> property are correctly caught by the <code>UnHandledException</code> handler in the App.xaml.cs. However, the exception being thrown in the <code>ShellModel.OpenFile</code> property after using the <code>OpenFileDialog</code> is being suppressed.</p> <p>Why would these exceptions be suppressed?</p> <p><strong>App.xaml.cs</strong></p> <pre><code>using System; using System.Text; using System.Windows; namespace ExceptionTest { /// &lt;summary&gt; /// Interaction logic for App.xaml /// &lt;/summary&gt; public partial class App : Application { protected override void OnStartup( StartupEventArgs e ) { base.OnStartup( e ); AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; } private void OnUnhandledException( object sender, UnhandledExceptionEventArgs e ) { var ex = e.ExceptionObject as Exception; if( ex == null ) { MessageBox.Show( string.Format( "Null Exception: {0}", e ) ); return; } var sb = new StringBuilder(); sb.AppendLine( "An unhandled exception was encountered. Terminating now." ); sb.AppendLine(); sb.AppendLine( "Exception:" ); sb.AppendLine( ex.Message ); MessageBox.Show( sb.ToString(), "Whoops...", MessageBoxButton.OK, MessageBoxImage.Error ); Environment.Exit( 1 ); } } } </code></pre> <p><strong>Shell.xaml</strong></p> <pre><code>&lt;Window x:Class="ExceptionTest.Shell" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Model="clr-namespace:ExceptionTest" Title="Exception Test" Height="350" Width="350" WindowStartupLocation="CenterScreen"&gt; &lt;Window.DataContext&gt; &lt;Model:ShellModel x:Name="Model" /&gt; &lt;/Window.DataContext&gt; &lt;StackPanel Orientation="Vertical" VerticalAlignment="Stretch"&gt; &lt;Button Click="OnCodeBehind" Margin="20" Content="Exception from code behind" Height="25" Width="250" /&gt; &lt;Button Click="OnThrowExeption" Margin="20" Content="Exception from Model" Height="25" Width="250" /&gt; &lt;Button Click="OnFindFile" Margin="20" Content="Exception from OpenFileDialog" Height="25" Width="250" /&gt; &lt;Label Content="{Binding OpenFile, Mode=TwoWay}" x:Name="OpenFile" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" /&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p><strong>Shell.xaml.cs / Model</strong></p> <pre><code>using System; using System.ComponentModel; using System.IO; using System.Reflection; using System.Windows; using Microsoft.Win32; namespace ExceptionTest { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class Shell : Window { private OpenFileDialog OpenDialog { get; set; } public Shell() { InitializeComponent(); OpenDialog = new OpenFileDialog(); string path = new Uri( Assembly.GetExecutingAssembly().CodeBase ).LocalPath; OpenDialog.InitialDirectory = Path.GetDirectoryName( path ); OpenDialog.Multiselect = false; OpenDialog.Title = "Find File"; OpenDialog.RestoreDirectory = true; } private void OnCodeBehind( object sender, RoutedEventArgs e ) { throw new Exception( "Exception from Code Behind." ); } private void OnThrowExeption( object sender, RoutedEventArgs e ) { Model.ThrowException = "Test"; e.Handled = true; } private void OnFindFile( object sender, RoutedEventArgs e ) { OpenDialog.ShowDialog( this ); string fileName = OpenDialog.FileName; if( !string.IsNullOrEmpty( fileName ) ) { OpenDialog.InitialDirectory = Path.GetDirectoryName( fileName ); OpenFile.Content = fileName; } } } public class ShellModel : INotifyPropertyChanged { private string _throwException; public string ThrowException { get { return _throwException; } set { _throwException = value; NotifyPropertyChanged( "ThrowException" ); throw new Exception( "Exception from Model." ); } } private string _openFile; public string OpenFile { get { return _openFile; } set { _openFile = value; NotifyPropertyChanged( "OpenFile" ); throw new Exception( "Exception from Model after using OpenFileDialog." ); } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged( String info ) { if( PropertyChanged != null ) { PropertyChanged( this, new PropertyChangedEventArgs( info ) ); } } } } </code></pre> <h2>Resolution</h2> <p>As noted in the answer, this isn't an OpenFileDialog issues, rather its a data binding issue.</p> <p>Bradley's answer and Hans possible duplicate link pointed to some great information. The links/articles didn't quite provide the resolution I came up with, re: I found there was another exception I could hook into: <code>AppDomain.CurrentDomain.FirstChanceException</code></p> <p>Here's a modified version of my <code>App.Xaml.cs</code>:</p> <pre><code>protected override void OnStartup( StartupEventArgs e ) { base.OnStartup( e ); AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; // The FirstChanceException will catch binding errors AppDomain.CurrentDomain.FirstChanceException += OnFirstChanceException; } private void OnFirstChanceException( object sender, FirstChanceExceptionEventArgs e ) { // do stuff } </code></pre> <p>The binding errors are now caught!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload