Why didn't this catch block catch an abandoned exception?

I have this code:

private void btn1_Click(object sender, EventArgs e) { try { Thrower(); } catch { MessageBox.Show("exception caught"); } } 

Which call calls this method:

 private void Thrower() { throw new OverflowException(); } 

Well, I'm not very experienced when it comes to handling exceptions, but I would have thought that this message box (โ€œexception caughtโ€) would be shown here. This is not true. Should it be? If not, what am I doing wrong? I misunderstood how this should work?

Thanks.

+4
source share
2 answers

I tested this and it works great for me. Are you sure you are really calling the btn1_Click method? Maybe you forgot to connect your events?

+9
source

Place a breakpoint inside your try / catch to see even if it got there (it is not)

+1
source

Source: https://habr.com/ru/post/1386134/


All Articles