Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current exception without having passing the variable?

I am looking for a way to retrieve the current exception without having to pass it as a variable.

Suppose the following code

public void MakeItFail()
{
    try
    {
        throw new FailException();
    }
    catch // Yes I'm aware that this shouldn't be done, but I don't want to go through all the code base and change it
    {
        ShowMessage("An error occured");
    }
}

public void ShowMessage(string message)
{
    // How can I retrieve the exception here
}

In the watch window, I can use $exception to get the current exception. Is there is a code equivalent?

like image 527
Pierre-Alain Vigeant Avatar asked May 07 '10 13:05

Pierre-Alain Vigeant


2 Answers

Try subscribing to this event when you first load your app.

AppDomain.CurrentDomain.FirstChanceException += (s, e) =>
{
    ShowMessage(e.Exception.Message);
};
like image 108
ChaosPandion Avatar answered Nov 11 '22 12:11

ChaosPandion


No, there isn't.

You need to use a parameter.

like image 33
SLaks Avatar answered Nov 11 '22 12:11

SLaks