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

Posted by Pierre-Alain Vigeant on Stack Overflow See other posts from Stack Overflow or by Pierre-Alain Vigeant
Published on 2010-05-07T13:18:41Z Indexed on 2010/05/07 13:28 UTC
Read the original article Hit count: 296

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about exception-handling