Turned off exceptions?

B

Brian

I have done something and I need to figure out how to undo it.

I somehow have turned off exceptions. Let me explain with an example.

I put some code into a form's textbox's DragDrop event handler.

It does the following cast:

this.Filename.text = (string) e.Data.GetData(DataFormats.FileDrop);

When I run the form and do a drag drop, nothing happens. So, I then wrapped
it around a try block:

try
{
this.Filename.text = (string) e.Data.GetData(DataFormats.FileDrop);
}
catch (Exception ex)
{
this.Filename.Text = "Unknown file.";
}

and guess what, the exception handling code runs! It says there that the
cast to string is invalid.


That's fine, but my question is, why didn't an "unhandled exception" box
pop up when it wasn't wrapped in a try block. Shouldn't my program have
died?

What's up?

--Brian
 
M

Morten Wennevik

Hi Brian,

The code is called by Windows and not your application. Any exception in
the code will be handled by Windows itself and Windows chooses not to
notify you that the drop failed. If you call the method directly you will
get the exception even without the try/catch block.
 
N

Nicholas Paldino [.NET/C# MVP]

Brian,

When controls fire events, if an exception in an event handler occurs,
it is gobbled up by the control that fired the event, and that is why you
never see it.

You see it when you place a try/catch block around the code in the event
handler because you intercept the event first.

Hope this helps.
 
B

Brian

When controls fire events, if an exception in an event handler
occurs, it is gobbled up by the control that fired the event, and that
is why you never see it.

You see it when you place a try/catch block around the code in the
event handler because you intercept the event first.

Hope this helps.

I guess this is just another good reason to wrap everything in a try block.
When the code didn't throw an exception, but it also didn't work, i was
stumped. I'm a little amazed in my readings i've never seen this, but oh
well, live and learn.

Thanks,

Brian
 

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

Similar Threads

Drag and drop problem 6
Unhandled Exceptions 3
Catching Exceptions 5
Exception Handling Question 16
Threads and Exceptions 7
Catching lots of exceptions 6
Exception to exceptions 6
catch exceptions 2

Top