Turned off exceptions?

  • Thread starter Thread starter Brian
  • Start date Start date
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
 
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.
 
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.
 
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
 
Back
Top