J
Jeff
I'm just starting to work with UserControls and have a question on the
behavoir of exceptions within UserControls and VS.Net 2003.
I have noticed that when a UserControl throws an exception from an event
(i.e. Click event), VS.Net will enter debug mode on the exact line that
threw the exception in the UserControl. You can see the complete call stack
up to the Click event, all local variables from the Click event, etc.
However, if an exception is thrown from the Paint event, VS.Net will enter
debug mode on the line following Application.Run(new Form1()); in the parent
form. The only information in the call stack is the Main() method and you
have no clue as to what line of code threw the exception nor access to local
variable from the Paint event.
Obviously the information from an exception in the Click event is much more
useful than the information from an exception in the Paint event.
Here is how to reproduce this behavior:
1. Create a new C# windows application called ExceptionTest
2. Create a new UserControl called ExceptionControl1
3. Add an event handler for the Click event and Paint event
4. Add the following line of code to the click event:
throw new OverflowException();
5. Build the project
6. Add the ExceptionControl1 to Form1
7. Run the project
8. When you click the area where you placed the ExceptionControl1 an
exception will be thrown and the VS.net debugger should take you right to
the Click event code.
9. Stop debugging and move the one line of code from the Click event to the
Paint event.
10. Run the project
11. The application should throw an exception immediatley and the VS.Net
debugger should dump you in the Form1.Main() method. Pretty useless.
My question is, why is there a difference and how can I use VS.Net to begin
debugging within the Paint method when an exception is thrown?
behavoir of exceptions within UserControls and VS.Net 2003.
I have noticed that when a UserControl throws an exception from an event
(i.e. Click event), VS.Net will enter debug mode on the exact line that
threw the exception in the UserControl. You can see the complete call stack
up to the Click event, all local variables from the Click event, etc.
However, if an exception is thrown from the Paint event, VS.Net will enter
debug mode on the line following Application.Run(new Form1()); in the parent
form. The only information in the call stack is the Main() method and you
have no clue as to what line of code threw the exception nor access to local
variable from the Paint event.
Obviously the information from an exception in the Click event is much more
useful than the information from an exception in the Paint event.
Here is how to reproduce this behavior:
1. Create a new C# windows application called ExceptionTest
2. Create a new UserControl called ExceptionControl1
3. Add an event handler for the Click event and Paint event
4. Add the following line of code to the click event:
throw new OverflowException();
5. Build the project
6. Add the ExceptionControl1 to Form1
7. Run the project
8. When you click the area where you placed the ExceptionControl1 an
exception will be thrown and the VS.net debugger should take you right to
the Click event code.
9. Stop debugging and move the one line of code from the Click event to the
Paint event.
10. Run the project
11. The application should throw an exception immediatley and the VS.Net
debugger should dump you in the Form1.Main() method. Pretty useless.
My question is, why is there a difference and how can I use VS.Net to begin
debugging within the Paint method when an exception is thrown?