Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this catch block not catching the thrown exception?

I have this code:

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

Which calls this method:

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

Well I'm not very experienced when it comes to exception handling, but I would have thought that this message box ("exception caught") would show here. It doesn't. Should it be? If not, what am I doing wrong? Have I misunderstood how this is supposed to work?

Thanks.

like image 432
user738383 Avatar asked Dec 27 '22 09:12

user738383


1 Answers

I tested this and it works fine for me. Are you sure you sure the btn1_Click method is actually being called? Maybe you forgot to wire up your events?

like image 97
Brian Avatar answered Jan 24 '23 08:01

Brian