Preface:
In WPF, Converter is a frequently used tool, because the data bound to XAML is not necessarily the data we need.
Problem:
If an exception is thrown in Converter, the program crashes and the uncaptured exception is processed in a centralized manner.
Exploration process: 1. UnhandledException
Exceptions that are not handled will inevitably occur in the program. If these exceptions are not handled, they will be first thrown to our main program,
If it has not been processed, it will be thrown to the. Net Framework, and eventually the program will be suspended.
Therefore, I often focus on this part of exceptions that have not been processed to prevent program failures.
For more information, see CSharp UnhandledException.
When an unhandled exception is caught, a message box is displayed and printed to the log.
Theoretically, this can capture exceptions thrown by all programs. However, when an exception is thrown in Converter, the program crashes.
Through Debug, the crash exception is StackOverflowException.
2. StackOverflowException
On the one hand, the name of this exception is a website I have already visited. On the other hand, this exception cannot be captured.
This is an explanation on MSDN: StackOverflowException.
In addition, in my experience, this exception usually indicates that an endless loop occurs in the program.
However, when I checked the program, I found that I did not write such code.
The error occurs in MessageBox. Show. (That is, when I handle uncaptured exceptions in the main program, a message box is thrown ).
Exception ex = errorMsg = + ex.Message + Environment.NewLine += ;
Comment out this line and find that everything is normal and the program will not be suspended. It indicates that MessageBox causes StackOverflowException.
3. MessageBox
When MessageBox. Show ("");, the current thread is blocked, so theoretically only one MessageBox can be displayed at a time.
( i = ; i < ; i++
It indicates that MessageBox. Show is called multiple times, resulting in StackOverflowException.
4. Logger
Check the log and find that no log information is printed. It indicates that the log remains in MessageBox. Show,
So before the log can be printed, the program crashes.
5. Trace Converter
One Conjecture is that Converter is called multiple times: When the xaml parser finds that Converter throws an exception,
The Converter is called again, causing another exception to be thrown. After the main program is captured, MessageBox. Show is executed again,
So the program crashes.
Verification conjecture:
temp = Convert( value, Type targetType, ( writer = File.AppendText(+ + temp++ Exception( ConvertBack( value, Type targetType,
Run the wrong program again, no exception. The program crashes, but I also get the desired data.
9/12/2013 3:01:42 PM - 09/12/2013 3:01:42 PM - 19/12/2013 3:01:42 PM - 29/12/2013 3:01:42 PM - 39/12/2013 3:01:42 PM - 49/12/2013 3:01:42 PM - 59/12/2013 3:01:42 PM - 69/12/2013 3:01:42 PM - 79/12/2013 3:01:42 PM - 89/12/2013 3:01:42 PM - 99/12/2013 3:01:43 PM - 109/12/2013 3:01:43 PM - 119/12/2013 3:01:43 PM - 129/12/2013 3:01:43 PM - 139/12/2013 3:01:43 PM - 14
As I guessed, Converter was indeed called 15 times, which is also the cause of program crash.
Conclusion:
1. When MessageBox. Show is called multiple times at the same time, StackOverflowException may occur.
I don't know how to verify this, but it seems like this.
2. When xaml finds that Converter throws an exception, it will continue to execute the Convert method for up to 15 times.
I have no idea what this means. If an exception has been thrown, no exception will occur when I call it again?
3. Try to handle exceptions in Converter as much as possible, otherwise it may cause unnecessary troubles.
The writing time is longer than the research time. I hope it will be helpful to everyone. I hope my experts can answer my questions.
Reference: Exception in WPF's Converter