none
Try catch RRS feed

  • Question

  • Hello. I've been doing a ton of research and have tried different things and am not able to put my Argument in the right place. If I leave it where it is it won't download most of the information from the classical music. If I take out the try - catch statemnet then it will download just fine. However if thr'es no CD i.e. out of print. Then it will give me an ArugumentNullException. So how can I do it so I can get my pop and classical informationl? Any help would be greatly appreciated,

    -PCrider.

    Her's the code I'm using now

     //frmPreview.lblUPC.Text = amazonItems[0].ASIN;
          frmPreview.txtAlbum.Text = amazonItems[lstMusic.SelectedIndex].ItemAttributes.Title;
          try
          {
            
            frmPreview.txtArtist.Text = String.Join("", amazonItems[lstMusic.SelectedIndex].ItemAttributes.Artist);
    
         
    
            //frmPreview.cboLabel.Text = amazonItems[0].ItemAttributes.Label;
            //frmPreview.genreTextBox.Text = amazonItems[lstMusic.SelectedIndex].ItemAttributes.Genre;
            //frmPreview.cboSub.Text = amazonItems[0].ItemAttributes.MusicalStyle;
            frmPreview.formatTextBox.Text = amazonItems[lstMusic.SelectedIndex].ItemAttributes.Binding;
            //frmPreview.txtInfo.Text = String.Join("", amazonItems[0].ItemAttributes.Format);
            frmPreview.txtReleaseDate.Text = DateTime.Parse(amazonItems[lstMusic.SelectedIndex].ItemAttributes.ReleaseDate).ToString("MMM, dd yyyy");
    
    
    
            // Format : 07 / 03 / 2004
            //formattedDate = frmPreview.releaseDateDateTimePicker.Value.ToString("dd / MM / yyyy")
            frmPreview.txtNumberOfDiscs.Text = amazonItems[lstMusic.SelectedIndex].ItemAttributes.NumberOfDiscs;
            //frmPreview.txtNumberOfTracks.Text = amazonItems[0].ItemAttributes.NumberOfTracks;
            //Statuslabel.Text = amazonItems[0].ItemAttributes.Title;
    
            //ItemAttributesCreator c = default(ItemAttributesCreator); string dlm = null;
            //if (frmPreview.txtArtist.Text == null)
    
            //frmPreview.txtComposer.Text = amazonItems[lstMusic.SelectedIndex].ItemAttributes.Creator.ToString();
    
          }
          catch (ArgumentException)
          {
            //string composer;
          }
          if (amazonItems[lstMusic.SelectedIndex].ItemAttributes.Creator != null)
          {
            
            foreach (ItemAttributesCreator c in amazonItems[lstMusic.SelectedIndex].ItemAttributes.Creator)
            {
              
              string performer = null;
              string composer = null;
              try
              {
                switch (c.Role)
                {
                  case "Orchestra":
                    frmPreview.txtOrchestra.Text = c.Value;
                    break;
                  case "Composer":
                    frmPreview.txtComposer.Text = c.Value;
                    break;
    
                  case "Performer":
                    frmPreview.txtArtist.Text = c.Value;
                    if (string.IsNullOrEmpty(frmPreview.txtArtist.Text))
                      performer = "";
                    break;
                  case "Various Composers":
                    if (string.IsNullOrEmpty(frmPreview.txtComposer.Text))
                      composer = "";
                    else
                      composer = ", ";
                    frmPreview.txtComposer.Text += composer + c.Value;
    
                    break;
    
                    
                }
                 
              }
    Saturday, June 12, 2010 9:53 PM

Answers

  • Here's the correct answer. Take out try - catch and insert this code above string join like this

      {
            if (amazonItems[lstMusic.SelectedIndex].ItemAttributes.Artist != null)
              frmPreview.txtArtist.Text = String.Join("", amazonItems[lstMusic.SelectedIndex].ItemAttributes.Artist);
          }
    • Marked as answer by PCRider Thursday, June 17, 2010 12:16 PM
    Thursday, June 17, 2010 12:16 PM

All replies

  • Hi,

    use try/catch/finally

    Saturday, June 12, 2010 10:30 PM
  • Ok. Great. Now where do I put the catch and finally statements?

    -PCRider

    Saturday, June 12, 2010 11:06 PM
  • On which line does the ArgumentNullException occur?
    Sunday, June 13, 2010 1:16 AM
  • Adding to collegues hints, I also suggest to add nested try catch finally block, as Microsoft suggests. See Exceptions on MSDN or 70-536 Exam textbook (Chapter 1, lesson 2).

    In this way you can catch higher level exceptions like CD not present, out of print et similar, while in the inner try you catch more specific exceptions, relevant to the specific attributes you are querying.

    Example of use is:

    try {
          myBusinessClass myBusiness = ...; //Instantiate your arguments
          try {
                call all the relevant attributes here...
               }
          catch { specific exceptions on myBusinessClass... }
          finally { if needed by your logic... }
    catch
         { my constructor had some problems... myBusiness does not exist, CD not present, out of print, argumentNullException... }
    finally
         { if I opened some ports, comm channels, etc., I close them here }
    }

    Giuseppe

    Sunday, June 13, 2010 11:30 AM
  • Here's what I've tried so far. However it keeps giving me the ArgumentNullException even with CD's in print. The problem is only with Classical not pop. Here's the modified code:

    //frmPreview.lblUPC.Text = amazonItems[0].ASIN;
    
    frmPreview.txtAlbum.Text = amazonItems[lstMusic.SelectedIndex].ItemAttributes.Title;
    
    try
    
    {
    
    frmPreview.txtArtist.Text = String.Join("", amazonItems[lstMusic.SelectedIndex].ItemAttributes.Artist);
    
     
    
     
    
    //frmPreview.cboLabel.Text = amazonItems[0].ItemAttributes.Label;
    
    //frmPreview.genreTextBox.Text = amazonItems[lstMusic.SelectedIndex].ItemAttributes.Genre;
    
    //frmPreview.cboSub.Text = amazonItems[0].ItemAttributes.MusicalStyle;
    
    frmPreview.formatTextBox.Text = amazonItems[lstMusic.SelectedIndex].ItemAttributes.Binding;
    
    //frmPreview.txtInfo.Text = String.Join("", amazonItems[0].ItemAttributes.Format);
    
    frmPreview.txtReleaseDate.Text = DateTime.Parse(amazonItems[lstMusic.SelectedIndex].ItemAttributes.ReleaseDate).ToString("MMM, dd yyyy");
    
     
    
     
    
    // Format : 07 / 03 / 2004
    
    //formattedDate = frmPreview.releaseDateDateTimePicker.Value.ToString("dd / MM / yyyy")
    
    frmPreview.txtNumberOfDiscs.Text = amazonItems[lstMusic.SelectedIndex].ItemAttributes.NumberOfDiscs;
    
    //frmPreview.txtNumberOfTracks.Text = amazonItems[0].ItemAttributes.NumberOfTracks;
    
    //Statuslabel.Text = amazonItems[0].ItemAttributes.Title;
    
    //ItemAttributesCreator c = default(ItemAttributesCreator); string dlm = null;
    
    //if (frmPreview.txtArtist.Text == null)
    
    //frmPreview.txtComposer.Text = amazonItems[lstMusic.SelectedIndex].ItemAttributes.Creator.ToString();
    
    }
    
    catch (ArgumentNullException)
    
    {
    
    MessageBox.Show("Out of print try another CD");
    
    }
    
    finally
    
    {
    
    if (amazonItems[lstMusic.SelectedIndex].ItemAttributes.Creator != null)
    
    {
    
    foreach (ItemAttributesCreator c in amazonItems[lstMusic.SelectedIndex].ItemAttributes.Creator)
    
    {
    
    string performer = null;
    
    string composer = null;
    
    try
    
    {
    
    switch (c.Role)
    
    {
    
    case "Orchestra":
    
    frmPreview.txtOrchestra.Text = c.Value;
    
    break;
    
    case "Composer":
    
    frmPreview.txtComposer.Text = c.Value;
    
    break;
    
    case "Performer":
    
    frmPreview.txtArtist.Text = c.Value;
    
    if (string.IsNullOrEmpty(frmPreview.txtArtist.Text))
    
    performer = "";
    
    break;
    
    case "Various Composers":
    
    if (string.IsNullOrEmpty(frmPreview.txtComposer.Text))
    
    composer = "";
    
    else
    
    composer = ", ";
    
    frmPreview.txtComposer.Text += composer + c.Value;
    
    break;
    
     
    
     
    
     
    
    }
    
    }
    
    catch (Exception X)
    
    {
    
    MessageBox.Show("Unable to transfer info to preview form");
    
    }
    
    ArrayList arr = new ArrayList();
    
    foreach (AmazonWS.TracksDisc td in amazonItems[lstMusic.SelectedIndex].Tracks)
    
    {
    
    foreach (AmazonWS.TracksDiscTrack tdt in td.Track)
    
    {
    
    frmPreview.lstTracks.Items.Add(string.Format("{1} - {2}", td.Number, tdt.Number, tdt.Value + " -|00:00:00|"));
    
    arr.Add(new TimeSpan(0, 0, 0));
    
     
    
     
    
    }
    
    frmPreview.lstTracks.Tag = arr;
    
    }
    
     
    
    • Marked as answer by PCRider Tuesday, June 15, 2010 9:40 PM
    • Unmarked as answer by PCRider Thursday, June 17, 2010 12:16 PM
    Sunday, June 13, 2010 7:59 PM
  • Here's the correct answer. Take out try - catch and insert this code above string join like this

      {
            if (amazonItems[lstMusic.SelectedIndex].ItemAttributes.Artist != null)
              frmPreview.txtArtist.Text = String.Join("", amazonItems[lstMusic.SelectedIndex].ItemAttributes.Artist);
          }
    • Marked as answer by PCRider Thursday, June 17, 2010 12:16 PM
    Thursday, June 17, 2010 12:16 PM