-3

When I made a LostFocus-event for a TextBox, a NotImplementedException() is added. How do I catch this exception? My code is:

void marks_LostFocus(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

Edited

I m sorry to be unclear the question that i wanted to ask is that when i create an event of lostFocus the VS automatically adds this line. Academically speaking. where can i catch this exception and what is use of it. why does the VS automatically adds this line

user2214731
  • 59
  • 1
  • 5
  • Where do you wish to catch this exception? Catch all unhandled exceptions? http://stackoverflow.com/questions/5762526/how-can-i-make-something-that-catches-all-unhandled-exceptions-in-a-winforms-a – Adriaan Stander Mar 27 '13 at 08:38

4 Answers4

1

Wrap it around try/catch?

void marks_LostFocus(object sender, EventArgs e)
{ 
    try
    {
        throw new NotImplementedException();
    }
    catch(Exception ex)
    {
        // handle ex
    }
}
SeToY
  • 5,777
  • 12
  • 54
  • 94
0

Only the method which triggers the event can catch all unhandled exceptions that the subscriber methods throws. There is also a fallback solution which you can use:

Subscribe on the Application.ThreadException event.

In Program.cs:

Application.ThreadException += OnThreadException


private static void OnThreadException(object sender, ThreadExceptionEventArgs e)
{
    MessageBox.Show(e.Exception.ToString());
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372
0

First of all - you can implement this method.

But if you want to handle unhandled exceptions, you can try to use unhandled exception event of your AppDomain or any other global exception handler

JleruOHeP
  • 10,106
  • 3
  • 45
  • 71
0

Catching this exception from within the handler itself it nonsensical - managing it from calling code could make sense. However, it would seem you simply don't need it; given the context in which I see this, it serves precisely no purpose - you can prevent this exception by removing the code.

What you do in this block is implement your own logic. That is, you implement it.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129