I noted the other day at work a piece of code that was failing. Not that it
was crashing mind, but that it simply wasn't doing anything at all.
The event that triggered this non-event was a DragDrop event.
When going through the debugger I immediately noticed that an object was
null and that an operation was going to be performed on that object
(arrrgghhh NullRef!).
The operation was performed but the code just jumped out, it didn't die as
i'd assume.
I searched high and low for an evil:
try
{
DoDragDrop();
}
catch{}
But found none. I then replicated the problem in a simple Winform app.
I found that OnDragDrop handles unhandled exceptions automagically.
Why on earth is this? I'd expect the application to crash out.
// just copy and paste the code below into an empty .cs file
// and chuck a breakpoint on the exception and drag something onto the form
// (e.g. a folder from Windows Explorer)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
public Form1()
{
this.SuspendLayout();
this.AllowDrop = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.DragDrop += new
System.Windows.Forms.DragEventHandler(this.Form1_D ragDrop);
this.DragOver += new
System.Windows.Forms.DragEventHandler(this.Form1_D ragOver);
this.ResumeLayout(false);
}
private void Form1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
throw new NullReferenceException("Oh my everything has gone
horribly wrong");
}
}
}
// Simon