Skip to content

Derive from Exception or ApplicationException

Context: Custom exceptions should derive from System.Exception. Deriving from ApplicationException is no longer recommended (the .NET Framework guidelines have changed). Derive directly from Exception unless you have a specific reason to use a more specific base class. This ensures your exception integrates seamlessly with the runtime.

using System;
// Recommended way
public class MyCustomException : Exception
{
public MyCustomException() { }
public MyCustomException(string message) : base(message) { }
public MyCustomException(string message, Exception inner) : base(message, inner) { }
}
// Not recommended (but still works)
public class OldStyleException : ApplicationException
{
public OldStyleException() { }
public OldStyleException(string message) : base(message) { }
}
Terminal window
// No output; compilation succeeds.
  • ApplicationException was intended to distinguish between framework exceptions and custom ones, but it is rarely used today.
  • Always mark your exception with [Serializable] if you plan to use it across remoting or serialization.

InvalidOperationException – A built-in exception that is often used as a base for domain-specific invalid states.
See .NET docs on exception inheritance.