views:

46

answers:

4

Alright, so based on another question I asked about a program of mine not running, I'm trying to put this code into my program that will hopefully point out any unhandled exceptions to me. However, it's not working the way I've written it.

private void FileSort_Load(object sender, EventArgs e)
    {
        this.Size = new System.Drawing.Size(693, 603);
        this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Main_FormClosing);
        System.AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 
        //insert here anything that will occur on the program's start
    }

    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        throw new NotImplementedException();
        // MessageBox.Show(e.ExceptionObject); //why doesn't this work?!?!?!?!?!?!?!?
    }

So my issue is obviously with the Messagebox.Show() function in the code above. It tells me that it cannot convert from 'object' to 'string'. I tried using the ToString function, but that causes more problems...why won't this work the way it is? (I received this suggestion as an answer to my other question, but I'm not too familiar with this (as I'm new to C#, and OOP as a whole) so I'm not sure if I did anything wrong, I just let VS 2010 fill it in for me after typing System.AppDomain.CurrentDomain.UnhandledException +=)

Any help would be appreciated.

+4  A: 

That statement won't be reached, since there's a throw immediately above it. If you put:

void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    MessageBox.Show(e.ExceptionObject.ToString());
    throw new NotImplementedException();
}

it might do what you want. You need the ToString because Show doesn't take an object as the only parameter. You said this caused "more problems", but that's really too vague to help.

Matthew Flaschen
I think we assume this is done, otherwise the error message the OP mentions would never be raised.
James Gaunt
@James, what error message? The "cannot convert from 'object' to 'string'" is a compile-time error.
Matthew Flaschen
A: 

(Assuming you would comment the exception thow, and uncomment the line in question generally you can't pass an object to a method that expects a string, so you need to pass the string you want to the Show method.

Then the question is what string do you want it to Show? All exceptions have a Message property. This is probably the string you want to show, so change the code to

MessageBox.Show((Exception)(e.ExceptionObject).Message)

Does that do what you want? If not can you explain exactly what you want it to do?

James Gaunt
e.ExceptionObject is declared as an object.
recursive
Ah! good point, thanks
James Gaunt
Oh well... seems the problem is more with the method never being called anyway
James Gaunt
I've moved it ahead of the throw, but no message box appears anyway...
rar
A: 

MessageBox.Show() takes a string, while e.ExceptionObject is an object as per the documentation.

You could try this:

MessageBox.Show(e.ExceptionObject.ToString()); 
recursive
This is what I realized, I was using the ToString() function incorrectly. However, no messagebox pops up even when I put this message before the throw, so this would imply to me that either I'm doing it wrong, or that there are no unhandled exceptions...
rar
A: 

Instead of trying to show a message box, you should probably write to the windows event log. Then, just log every step of the process to nail down where the problem lies.

This link might give you a little help.

Chris Lively