1

I have a custom exception class, inherited from Exception and a console application where the exception is thrown. My custom exception overrides the ToString() method, but when the exception is thrown, the message and stack trace print to console, but the overridden ToString() method does not seem to be called.

I know how to create a default exception handler (.NET Global exception handler in console application). However, what is the body of the default handler for console application if the custom one is not specified?

Community
  • 1
  • 1
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80

1 Answers1

0

I'm not sure what do you want to achieve by overriding .ToString() method, but if you are trying to set a custom message I would recommend to use the constructor:

public class CustomException : Exception 
{
    public CustomException(string message) : base(message)
    {

    }
}

or override Message property:

public class CustomException : Exception 
{
    public override string Message
    {
        get
        {
            return "Something bad happened";
        }
    }
}

Answering on the question about UnhandledException event - I believe that handlers are launched on CLR level when an unhandled exception is found for specific domain therefore it is quite impossible to look if there are default handlers on it. I guess that there is no point to even have those.

Oleksii Aza
  • 5,368
  • 28
  • 35