Re: Throwing Exceptions
From: David Levine (noSpamdlevineNNTP2_at_wi.rr.com)
Date: 08/20/04
- Next message: Richard: "RE: Callback methods are executed in different threads. How have the m"
- Previous message: Richard: "RE: Retrieving IL Code from Assembly with C#"
- In reply to: Ignacio Machin \( .NET/ C# MVP \): "Re: Throwing Exceptions"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 19 Aug 2004 20:03:32 -0500
That actually makes sense in a weird way even if it is bad practice. If you
consider that a drag-drop event can originate from another application, you
would not want an unhandled exception in the drop target to cause the drop
source to unexpectedly terminate. This could happen because even though the
process context may be different, they are running on what amounts to the
same thread of execution, and the exception would likely propagate back up
the call stack to the original caller.
I think a better strategy would have been to only swallow the exception if
the drop source was in a different process, otherwise it should have been
allowed to propagate up the call stack.
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:ep7Ov2hhEHA.1964@tk2msftngp13.phx.gbl...
> Hi Ron,
>
> I just ran it and got the same result, it does not throw the exception !
> This is the first time I see this. I'm not expert in CLR code but I
believe
> it's Control.DoDragDrop the one that is "eating" the exceptions.
>
> Let see if somebody else here test the code and we can know where the
secret
> lies.
>
> Cheers,
>
> --
> Ignacio Machin,
> ignacio.machin AT dot.state.fl.us
> Florida Department Of Transportation
>
>
>
> "Ron Green" <Ron Green@discussions.microsoft.com> wrote in message
> news:316AF1F5-8EDC-4833-85C6-BE45F053AFD5@microsoft.com...
> > Here you go:
> >
> > using System;
> > using System.Drawing;
> > using System.Collections;
> > using System.ComponentModel;
> > using System.Windows.Forms;
> > using System.Data;
> >
> > namespace TestDragDrop
> > {
> > /// <summary>
> > /// Summary description for Form1.
> > /// </summary>
> > public class Form1 : System.Windows.Forms.Form
> > {
> > private System.Windows.Forms.Label lblDest;
> > private System.Windows.Forms.Label lblSource;
> > private System.Windows.Forms.TextBox txtSource;
> > private System.Windows.Forms.Button button1;
> > /// <summary>
> > /// Required designer variable.
> > /// </summary>
> > private System.ComponentModel.Container components = null;
> >
> > public Form1()
> > {
> > //
> > // Required for Windows Form Designer support
> > //
> > InitializeComponent();
> >
> > //
> > // TODO: Add any constructor code after InitializeComponent call
> > //
> > }
> >
> > /// <summary>
> > /// Clean up any resources being used.
> > /// </summary>
> > protected override void Dispose( bool disposing )
> > {
> > if( disposing )
> > {
> > if (components != null)
> > {
> > components.Dispose();
> > }
> > }
> > base.Dispose( disposing );
> > }
> >
> > #region Windows Form Designer generated code
> > /// <summary>
> > /// Required method for Designer support - do not modify
> > /// the contents of this method with the code editor.
> > /// </summary>
> > private void InitializeComponent()
> > {
> > this.lblDest = new System.Windows.Forms.Label();
> > this.lblSource = new System.Windows.Forms.Label();
> > this.txtSource = new System.Windows.Forms.TextBox();
> > this.button1 = new System.Windows.Forms.Button();
> > this.SuspendLayout();
> > //
> > // lblDest
> > //
> > this.lblDest.AllowDrop = true;
> > this.lblDest.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
> > this.lblDest.Location = new System.Drawing.Point(24, 24);
> > this.lblDest.Name = "lblDest";
> > this.lblDest.Size = new System.Drawing.Size(320, 24);
> > this.lblDest.TabIndex = 0;
> > this.lblDest.DragEnter += new
> > System.Windows.Forms.DragEventHandler(this.lblDest_DragEnter);
> > this.lblDest.DragDrop+=new DragEventHandler(lblDest_DragDrop);
> > //
> > // lblSource
> > //
> > this.lblSource.Location = new System.Drawing.Point(32, 112);
> > this.lblSource.Name = "lblSource";
> > this.lblSource.Size = new System.Drawing.Size(296, 32);
> > this.lblSource.TabIndex = 1;
> > this.lblSource.Text = "Type in text in the text box below then drag it
> > into the outlined label.";
> > //
> > // txtSource
> > //
> > this.txtSource.Location = new System.Drawing.Point(32, 152);
> > this.txtSource.Name = "txtSource";
> > this.txtSource.Size = new System.Drawing.Size(296, 20);
> > this.txtSource.TabIndex = 2;
> > this.txtSource.Text = "Your text here...";
> > this.txtSource.MouseMove += new
> > System.Windows.Forms.MouseEventHandler(this.txtSource_MouseMove);
> > //
> > // button1
> > //
> > this.button1.Location = new System.Drawing.Point(360, 240);
> > this.button1.Name = "button1";
> > this.button1.TabIndex = 3;
> > this.button1.Text = "button1";
> > this.button1.Click += new System.EventHandler(this.button1_Click);
> > //
> > // Form1
> > //
> > this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
> > this.ClientSize = new System.Drawing.Size(504, 310);
> > this.Controls.Add(this.button1);
> > this.Controls.Add(this.txtSource);
> > this.Controls.Add(this.lblSource);
> > this.Controls.Add(this.lblDest);
> > this.Name = "Form1";
> > this.Text = "Form1";
> > this.ResumeLayout(false);
> >
> > }
> > #endregion
> >
> > /// <summary>
> > /// The main entry point for the application.
> > /// </summary>
> > [STAThread]
> > static void Main()
> > {
> > Application.Run(new Form1());
> > }
> >
> > private void lblDest_DragEnter(object sender,
> > System.Windows.Forms.DragEventArgs e)
> > {
> > if (e.Data.GetDataPresent(typeof(string)))
> > e.Effect = DragDropEffects.Copy;
> > else
> > e.Effect = DragDropEffects.None;
> > }
> >
> > private void lblDest_DragDrop(object sender, DragEventArgs e)
> > {
> >
> > // uncomment the appexception line. it won't blow up. who is catching
the
> > error?
> >
> > throw new Exception("this an exception");
> >
> > // if (e.Data.GetDataPresent(typeof(string)))
> > // {
> > // string text = (string)e.Data.GetData(typeof(string));
> > // this.lblDest.Text = text;
> > // }
> > }
> >
> > private void lblSource_MouseMove(object sender,
> > System.Windows.Forms.MouseEventArgs e)
> > {
> > this.lblSource.DoDragDrop(this.lblSource.Text, DragDropEffects.Copy);
> > }
> >
> > private void textBox1_MouseMove(object sender,
> > System.Windows.Forms.MouseEventArgs e)
> > {
> > this.lblSource.DoDragDrop(this.lblSource.Text, DragDropEffects.Copy);
> > }
> >
> > private void txtSource_MouseMove(object sender,
> > System.Windows.Forms.MouseEventArgs e)
> > {
> > this.txtSource.DoDragDrop(this.txtSource.Text, DragDropEffects.Copy);
> > }
> >
> > private void button1_Click(object sender, System.EventArgs e)
> > {
> > throw new ApplicationException("this an exception");
> > }
> > }
> > }
> >
> >
> >
> > "Ignacio Machin ( .NET/ C# MVP )" wrote:
> >
> > > Hi Ron,
> > >
> > > Weird, it should throw an exception, unless that somewhere there is a
> catch
> > > {} eating the exceptions, if so it would be a surprise for me.
> > > Do this:
> > > Put a break point in the event handler, probably what is happening is
> that
> > > it's not being called, if not post the code here so we can exec. it
> > >
> > > Cheers,
> > >
> > > --
> > > Ignacio Machin,
> > > ignacio.machin AT dot.state.fl.us
> > > Florida Department Of Transportation
> > >
> > > "Ron Green" <Ron Green@discussions.microsoft.com> wrote in message
> > > news:6CDA21E0-BC6C-42C9-872A-60195F91A3EF@microsoft.com...
> > > > Simple example:
> > > > Winform with three controls: label, textbox, button.
> > > >
> > > > Create DragDrop event handler for label. Enter text in textbox and
> drag it
> > > > onto label. Works, no problem.
> > > >
> > > > remove code from dragdrop event handler and replace it with:
> > > >
> > > > throw new ApplicationException("This is an exception");
> > > >
> > > > Copy this line into the button click event handler also. Run the
app.
> > > > Press the button and you get a message that you have thrown an
> unhandled
> > > > excepton.
> > > >
> > > > Try the drag and drop. Nothing happens.
> > > >
> > > > Why does the button click produce an unhandled exception message and
> > > > dragdrop does not?
> > >
> > >
> > >
>
>
- Next message: Richard: "RE: Callback methods are executed in different threads. How have the m"
- Previous message: Richard: "RE: Retrieving IL Code from Assembly with C#"
- In reply to: Ignacio Machin \( .NET/ C# MVP \): "Re: Throwing Exceptions"
- Messages sorted by: [ date ] [ thread ]