Solved

How to maintain the list of all the exceptions in a List<> object in C#?

Posted on 2011-09-06
5
206 Views
Last Modified: 2012-05-12
Hi,

I need to maintain a list of all exceptions in a List<> object. In the attached code below, I have a method 'Import' which will invoke another method 'SetField' in a for loop and I need to be able to capture any exceptions and continue with invoking the 'SetField' for the rest of the items. In the calling program, I need to check if there are any errors as below. But I am not able to access the list 'ErrorMessages' on the 'errors' object in the calling program. I am missing something here. Can anybody please provide any suggestions? Thank you.

//Calling program
Fields f = new Fields();
f.Import();
if(errors.ErrorMessages.Count > 0)
{
//loop through each message here.
}
public class Exceptions
    {
        List<string> ErrorMessages = new List<string>();

        public void Add(string field, int instance, string error)
        {
            ErrorMessages.Add(string.Format("Error '{0}' occured while setting the field {1} and instance {2}", error, field, instance));
        }
    }


class Fields
{

public Exceptions errors { get; set; }

public void Import()
{
	
	foreach(var item in fields)
	{
		try
		{
			SetField(item.field, item.instance)
		}
		catch(Exception x)
		{
			errors.Add(item.field, item.instance, x.Message)	
		}
	}
}

}

Open in new window

0
Comment
Question by:ipjyo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36493027
Add a property to access the data:

public class Exceptions
{
    private List<string> ErrorMessages = new List<string>();

    public void Add(string field, int instance, string error)
    {
        ErrorMessages.Add(string.Format("Error '{0}' occured while setting the field {1} and instance {2}", error, field, instance));
    }
    
    public List<string> Errors { get { return this.ErrorMessages } }
}

Open in new window


Then you would access it like this:
errors.Errors.Add(item.field, item.instance, x.Message)

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 36493031
Correction to the "access" example--it doesn't quite fit your code:
foreach (string err in errors.Errors)
{
    Console.WriteLine(err);
}

Open in new window

0
 

Author Comment

by:ipjyo
ID: 36495884
Thank you. Also, I am trying to add error messages using the property of type 'Exceptions' as below.

public Exceptions errors { get; set; }

errors.Add(item.field, item.instance, x.Message)      

Also, I will need to access this property to look for the errors in a different assembly as below. But I am not able to access the property 'Errors' of type <List>. The intellisense is showing only the property 'errors' but not the poperty defined in 'Exceptions'.

//Calling assembly
Fields f = new Fields();
f.Import();
if(errors.Errors.Count > 0)
{
//loop through each message here.
}

class Fields
{

public Exceptions errors { get; set; }

public void Import()
{
	
	foreach(var item in fields)
	{
		try
		{
			SetField(item.field, item.instance)
		}
		catch(Exception x)
		{
			errors.Add(item.field, item.instance, x.Message)	
		}
	}
}

}

Open in new window

0
 

Author Comment

by:ipjyo
ID: 36495912
I am sorry. my bad. I am able to access the property 'Errors' from a different assembly. Please ignore my previous comment.

Thanks for your help.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36496149
NP. Glad to help  = )
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
linq, c# 8 31
VS2010 Build fails to install 14 75
WPF user control poped up from a window 1 35
"The resource cannot be found" 1 22
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question