Unhandled exception while dragging

L

Ludovic SOEUR

I do not understand why when a dragging operation is started with
DoDragDrop, no exception is handled.
The following lines illustrate what I mean. In Form2_DragDrop event, I raise
an exception at the line "int b=1/a" (so the message After Exception will
not be shown). Why this exception will be unhandled ?

using System;
using System.Windows.Forms;

public class Form2 : System.Windows.Forms.Form {
[STAThread]
static void Main() {
Application.Run(new Form2());
}

public Form2() {
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.Form2_MouseDown);
this.DragDrop += new
System.Windows.Forms.DragEventHandler(this.Form2_DragDrop);
this.DragEnter += new
System.Windows.Forms.DragEventHandler(this.Form2_DragEnter);
this.AllowDrop = true;
}

private void Form2_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e) {
DoDragDrop(this,DragDropEffects.All);
}

private void Form2_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e) {
e.Effect=DragDropEffects.Copy;
}

private void Form2_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e) {
int a=0;
MessageBox.Show("Before exception");
int b=1/a;
MessageBox.Show("After exception");
}
}


Ludovic SOEUR
 
M

Morten Wennevik

Hi Ludovic,

Form2_DragDrop is called by Windows, so any unhandled exception will be
handled by Windows, so nothing appears to have happened.

If you on the other hand call Form2_DragDrop directly you will get the
Exception.
 
L

Ludovic SOEUR

Thanks for your answer that helped me to understand why it was not working.

But is there a way to handle this exception ? Not with a try catch in
Form2_DragDrop event with a message box but with a throw to the global
application ?

Thanks in advance.
Ludovic Soeur.
 
M

Morten Wennevik

Not to my knowledge, though I may be very wrong on this. Strictly
speaking, it is Windows that has performed an illegal action when running
part of your code, not your application.


Thanks for your answer that helped me to understand why it was not
working.

But is there a way to handle this exception ? Not with a try catch in
Form2_DragDrop event with a message box but with a throw to the global
application ?

Thanks in advance.
Ludovic Soeur.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top