Search code examples
c#winformsexceptionrichtextboxmessagebox

Catch all exceptions in a RichTextBox


I'm trying to load an xml file into the interface and there may be many exceptions based on data in Xml file, So I want to catch all the exceptions at once. I got around 15 exceptions and display it once RichTextBox or Something else or in a MessageBox.

for (int i = 0; i < this.SortedLaneConfigs.Count; i++)
    {
         if(this.SortedLaneConfigs[i].CheckConsistency())
            {
                throw new DataConsistencyException(String.Format("Lane #{0} NOT consistent : {1}", i, e.Message)
            }
    }


if (this.SortedLaneConfigs[i - 1].EndB > this.SortedConfigs[i].BeginB)
    {
        throw new DataConsistencyException(String.Format("Lanes {0} & {1}  overlap", i - 1, i));
    }

    this.SortedLaneConfigs.ForEach(
        laneConfig =>
        {
            if (this.SortedLaneConfigs.FindAll(item => item.Id == laneConfig.Id).Count != 1)
                {
                    new DataConsistencyException(String.Format("Id \"{0}\" present more than once", laneConfig.Id));
                }
        });

I know, I can catch exception and display it in a message box, in this normal way.

 try
    {
         this.SortedLaneConfigs[i].CheckConsistency();
    }
catch (Exception e)
    {
        MessageBox.Show("Error message below: \n\"" + String.Format("Configs #{0} NOT consistent : {1}", SortedLaneConfigs[i].Id, e.Message) + "\"", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

I googled it and i found these 2 links, link1:http://blogs.elangovanr.com/post/Catch-multiple-Exceptions-together-in-C.aspx link2: Catch multiple exceptions at once?

How can i adapt the suggested solution from those two links to display all the exceptions at once in RichTextBox or or Something else or in a messageBox. Please help me.


Solution

  • You can concatenate the Exception.Message strings and display them wherever you like: First create StringBuilder instance before you enter your method(s):

    StringBuilder exBuilder = new StringBuilder();
    

    Then execute your method(s) and append exceptions messages:

    try
    {
             this.SortedLaneConfigs[i].CheckConsistency();
    }
    catch (Exception e)
    {
            exBuilder.Append("Error message below: \n\"" + String.Format("Configs #{0} NOT consistent : {1}", SortedLaneConfigs[i].Id, e.Message) + "\"");
            exBuilder.Append(Environment.NewLine);
    }
    

    And after you finish you can get the string with exBuilder.ToString();

    richTextBox1.Text = exBuilder.ToString();
    

    EDIT: Suppose that you have a Form which has RichTextbox and Button on it. If the Button initiates your methods, then the use case can be like this:

    public partial class Form1 : Form
    {
            StringBuilder exBuilder;
            public Form2()
            {
                InitializeComponent();
                exBuilder = new StringBuilder();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                exBuilder.Clear();
                MyMethod();
                //and all your other methods that have exBuilder.Append in their try-catch blocks
                richTextBox1.Text = exBuilder.ToString();
            }
    
            void MyMethod()
            {
                try
                {
                    //you code or whatever
                }
                catch(Exception e)
                {
                    exBuilder.Append("Error message below: \n\"" + String.Format("Configs #{0} NOT consistent : {1}", parameter, e.Message) + "\"" + Environment.NewLine);
                }
            }
    }