Home > DeveloperSection > Forums > Exception message not working
Takeshi Okada
Takeshi Okada

Total Post:89

Points:629
Posted on    December-23-2013 3:22 AM

 C# C# 
Ratings:


 1 Reply(s)
 661  View(s)
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?


Pravesh Singh

Total Post:411

Points:2881
Posted on    December-23-2013 5:12 AM

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());

}


Don't want to miss updates? Please click the below button!

Follow MindStick