S Andrew
S Andrew

Reputation: 7198

Error: Invocation of the constructor

I have a application where I am planning to write down everything happening inside the code in a log file. So for that I made a wpf project named FileCreation where I am simply creating a file and then appending data to it. I also want to save the data when the application was started. Following is the code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Log("Application started on : " + DateTime.Now.ToString("dd-M-yyyy"));  
    }

    void Log(string data)
    {
        string path = @"C:\\Logs\\" + DateTime.Now.ToString("dd-M-yyyy") + ".txt";

        if (File.Exists(path))
        {
            using (StreamWriter sw = File.AppendText(path))
            {
                sw.WriteLine(data);

            }
        }
        else
        {
            StreamWriter myFile = new StreamWriter(path);
            myFile.WriteLine(data);
            myFile.Close();


        }
    }
}

So in the above code, I have created a function Log which accepts string data as parameter. It creates a file if the file is not created and then append data to it. I also want to record when the application was started so I thought of writing it also. So included the Log() after InitializeComponent(); because I think this is the first thing which initializes when application starts. But it is giving me below error:

enter image description here

I know why this error is coming because at starting it didn't know what is Log() function. But then where to define it. I also have one question. I was testing it first on my system and it was working perfectly. When I tested it on other system, then only it gave me this error.

Why it didn't gave me this error on my system.?

How can I handle it in my code.?

Upvotes: 0

Views: 707

Answers (1)

rmojab63
rmojab63

Reputation: 3631

You do not have enough permission to write in drive C:\ or the directory C:\Logs\ does not exists.

In order to make sure these are the problems or not, open the Exception Settings in Debug.Windows Menu and check all the checkboxes.

Note that, this exception shows that initialization of the MainWindow constructor has failed due to an unhandled exception. You can also check the InnerException part of the Exception window to see what was wrong with the code.

Edit

How can I handle it in my code

You should handle Dispatcher.UnhandledException method, for example before InitializeComponent(); line in your MainWindow. To do so, write:

 Dispatcher.UnhandledException += OnDispatcherUnhandledException; 

which requires the following method:

    void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        ShowError("An application error occurred.\nPlease check whether your data is correct and repeat the action. If this error occurs again there seems to be a more serious malfunction in the application.", e.Exception);

        e.Handled = true;
    }

in which, ShowError Tries to print Inner exceptions too:

    public static void ShowError(string message, Exception exp0)
    {
        Exception exp1 = exp0.InnerException;
        Exception exp2 = exp1 != null ? exp1.InnerException : null;
        Exception exp3 = exp2 != null ? exp2.InnerException : null;

        string mess = "unfortunately, no message is available.";
        string moremess = "";

        if (message != null)
        {
            mess = message;
            moremess = exp0.Message + "\n\n";
        }
        else if (exp0 != null)
        {
            mess = exp0.Message;
        }

        Exception exp = exp0.InnerException;
        while (exp != null)
        {
            moremess += exp.Message + "\n\n";
            exp = exp.InnerException;
        }

         MessageBox.Show(mess + Environment.NewLine + moremess);
    }

Hope it helps.

Upvotes: 1

Related Questions