When writing WPF application I often find that upon running new fucntions it will sometimes close, usually without an error message. This can make debugging challenging as I usually have to guess and experiment with the code to find the error. Is it possible to see the error messages (something like the error message of a console output), would it involve the output window?
Asked
Active
Viewed 876 times
-3
-
4Please [post code](https://stackoverflow.com/help/minimal-reproducible-example) that shows this “certain function”. – Dour High Arch Aug 22 '19 at 17:14
-
1Use the debugger (F5). It will break on the line that throws the exception which crashes your application and display the exception message. – BionicCode Aug 22 '19 at 17:54
-
Thank you @BionicCode exactly what I was looking for! Sorry for the poor question. – DaemonFire Aug 22 '19 at 21:47
1 Answers
3
You can catch exception by adding DispatcherUnhandledException
in App.xaml
<Application x:Class="Organizer.UI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Organizer.UI"
xmlns:toolkit="http://schemas.xceed.com/wpf/xaml/toolkit"
Startup="Application_Startup"
DispatcherUnhandledException="Application_DispatcherUnhandledException">
And when it create Application_DispatcherUnhandledException
in App.xaml.cs
you can show it like this
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("Error" + Environment.NewLine + e.Exception.Message, "Error");
e.Handled = true;
}