How to check if something is an .net exception or the custom exception
.net, c#, exception
Solution
Very simple:
var t = myException.GetType().FullName;
bool isSystemException = (t.StartsWith("System."));
Exception types in the .NET Framework are all in `System` or one of its subnamespaces.
EDIT: To make this slightly prettier, create an extension function to the `Exception` class:
public static bool IsSystemException(this Exception exception)
{
return (exception.GetType().FullName.StartsWith("System."));
}
Problem
Like in the question. I want to check if something on the collection of exceptions is my custom exception or is it the Exception class given by the .Net framework. Thnaks in advance for your help. \ Please note: I don't know what is the class name of my custom exception it could be called exceptionA, exceptionB or for example xyzException I have code like this: ``` public IEnumerable<Type> GetClassHierarchy(Type type) { if (type == null) yield break; Type typeInHierarchy = type; do { yield return typeInHierarchy; typeInHierarchy = typeInHierarchy.BaseType; } while (typeInHierarchy != null && !typeInHierarchy.IsInterface); } public string GetException(System.Exception ex) { if (ex == null) { return null; } if (ex.InnerException == null) { return ex.Message; } var exceptionHerarchy = GetClassHierarchy(ex.GetType()); var isMyException = exceptionHerarchy.Any(t => t != typeof(System.Exception)); if (isMyException) { return string.Format("{0};{1}", ex.Message, GetException(ex.InnerException)); } else { return GetException(ex.InnerException); } } ``` var isMyException = exceptionHerarchy.Any(t => t != typeof(System.Exception)); this is alays returning true because there is this type on the list probably