Update RichtextBox from exception of another class

Report
Question

Please briefly explain why you feel this question should be reported .

Report Cancel

Hi i want to update the Richtextbox text from another class’s exception. This is the sample code of what i am trying to do. I am just copying the relevant code.

The form1 class is calling CreateFolder of class CommonCreative. When commonCreative is falied then it should update the RichTextbox which is written in the catch exception of its method. But when there is an error, instead of writing to the Richtextbox in the catch mathod it is going to the Form1._Form1.update inside catch method but not writing to the Richtextbox and then going to the catch exception of the Form1 and printing "General Error" on the Richtextbox.

I want to write exception error from another class to the Richtext box.

public partial class Form1 : Form
{
    //some method ...
        try
        {
            FolderInfo newFolder = CommonCreative.CreateFolder(textFolderName.Text);
        }
        catch (Exception ex)
        {
            textBoxResult.Parent.Invoke((MethodInvoker)delegate
            {
                textBoxResult.AppendText("General Error");
                textBoxResult.AppendText(Environment.NewLine);
            });
        }
    //...

    public void update(string message)
    {
        textBoxResult.Parent.Invoke((MethodInvoker)delegate
        {
            textBoxResult.AppendText(message);
            textBoxResult.AppendText(Environment.NewLine);
        });

    }
}

public static class CommonCreative
{
    public static FolderInfo CreateFolder(string foldername)
    {
        try
        {
            //
            return folder;
        }
        catch (Exception ex)
        {
            // Failed - return null
            error = ex.Message;
            Form1._Form1.update("Error from the CommonCreative class");
            return null;
        }
    }
}
solved 0
1 Answer 4 views 0

Answer ( 1 )

    1
    November 25, 2016 at 5:02 am

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    It means the codes in catch part of CreateFolder, throws an exception.

    It’s not a good idea to write the excpetion messages that you catch in CreateFolder, in that rich text box of Form1.
    Instead you should let the exception bubble up or if you need a more custom exception message, you can wrap the exception in a new exception and throw the new exception.

    Then in the Form1 put call CreateFolder in a try/catch block and catch the (customized) exception and write its message in rich text box.

    For more information, read Design Guidelines for Exceptions.


    If you want to just fix the error and continue writing your application:

    Instead of trying to share an instance of your Form1 using a _Form1 property, You can find an instance of Form1 this way:

    var form1= Application.OpenForms.OfType<Form1>().FirstOrDefault();
    if(form1!=null)
        form1.update(ex.Message);
    
    Best answer

Leave an answer

Browse

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>