EXCEPTION MESSAGE NOT WORKING

Takeshi Okada

Total Post:89

Points:629
Posted by  Takeshi Okada
C# 
C#
 856  View(s)
Ratings:
Rate this:

public partial class Form1 : Form

{

    public Form1()

    {

        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

        try

        {

            string[] names = new string[2];

            string g = names[2];

        }

        catch(Exception error) {

            MessageBox.Show(error);

        }

    }

}

I don't know what’s wrong with it can’t seem to find the error. It would helpful if you could help me?
  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Exception message not working

    Hi Takeshi,

    There is no overload of Show which accepts an Exception as a parameter. You probably want to show the exception's Message property:

    try

    {

        string[] names = new string[2];

        string g = names[2];

    }

    catch(Exception error)

    {

        MessageBox.Show(error.Message);

    }

    // Index was outside the bounds of the array.

    Or possibly call ToString, which will typically provide you with a little more information than just the Message:

    try

    {

        string[] names = new string[2];

        string g = names[2];

    }

    catch(Exception error)

    {

        MessageBox.Show(error.ToString());

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!