Samuel Lee
Samuel Lee

Reputation: 77

C# Catching exception

Am quite new to this, so please help. I have the following image clicked code. However, an error will occur if the user does not click on a image. How can i do an error check to catch that if the user does not click on an image and attempts to proceed, a messagebox will display notifying him to click an image.

Error msg: The error is at "ListViewItem selectedItem = listView1.SelectedItems[0] Error Msg: Invalid Argument = Value of '0' is not valid for 'index

Below is my code:

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    int i = e.ProgressPercentage;
    object fbUserObject = e.UserState;
    if (fbUserObject is DataRow)
    {
        var fbUser = fbUserObject as DataRow;
        var item = new ListViewItem(fbUser["friend_name"].ToString());
        item.Tag = fbUser["friend_id"];
        item.ImageIndex = i;
        listView1.Items.Add(item);
    }
    else if (fbUserObject is Image)
    {
        imageList.Images.Add(fbUserObject as Image);
    }  
}

private void imageClicked(Object sender, System.EventArgs e)
{
    ListViewItem selectedItem = listView1.SelectedItems[0];
    selectedFBId = selectedItem.Tag as string;
    selectedFBName = selectedItem.Text;

    DialogResult dialogA = MessageBox.Show("Analyse employee data?", "SOC", MessageBoxButtons.YesNo);
    if (dialogA == DialogResult.Yes)
    {
        TargetEmployee.Text = "Selected Target: " + selectedFBName;
        pf.Show();
        ThreadPool.QueueUserWorkItem(LoadUserDetails);
    }
}

Upvotes: 2

Views: 217

Answers (3)

Sayse
Sayse

Reputation: 43330

You shouldn't catch an exception, you should handle when there aren't any selected items

if(listView1.SelectedItems.Count == 0)
{
 MessageBox.Show(this, "No image");
return;
}

Exceptions should be caught when you don't expect something to happen, if you are aware of a possible issue, you should handle that before it becomes an issue

Upvotes: 4

NibblyPig
NibblyPig

Reputation: 52952

You can use a try { } catch { } statement for your error handling.

Once you locate the line of code that generates an exception, you can wrap it into a block like this

try
{
   int a = int.Parse("pedantic"); // This throws an error because you cannot convert
}
catch (Exception e)
{
   // Handle your error here instead of crashing your program
}

Upvotes: 1

Ehsan
Ehsan

Reputation: 32729

change your code like this

 private void imageClicked(Object sender, System.EventArgs e)
    {
        if(listView1.SelectedItems.Count < 1)
                return;
        ListViewItem selectedItem = listView1.SelectedItems[0];
        selectedFBId = selectedItem.Tag as string;
        selectedFBName = selectedItem.Text;

        DialogResult dialogA = MessageBox.Show("Analyse employee data?", "SOC", MessageBoxButtons.YesNo);
        if (dialogA == DialogResult.Yes)
        {
            TargetEmployee.Text = "Selected Target: " + selectedFBName;
            pf.Show();
            ThreadPool.QueueUserWorkItem(LoadUserDetails);
        }
    }

Upvotes: 3

Related Questions