I want to make a custom Exception in C#, but in theory I do need to do a little parsing first before I can make a human readable ExceptionMessage.
The problem is that th
Consider the Microsoft Guidelines for creating new exceptions:
using System;
using System.Runtime.Serialization;
[Serializable]
public class CustomException : Exception
{
//
// For guidelines regarding the creation of new exception types, see
// https://msdn.microsoft.com/en-us/library/ms229064(v=vs.100).aspx
//
public CustomException()
{
}
public CustomException(string message) : base(message)
{
}
public CustomException(string message, Exception inner) : base(message, inner)
{
}
protected CustomException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
public static CustomException FromJson(dynamic json)
{
string text = ""; // parse from json here
return new CustomException(text);
}
}
Note the static factory method (not part of the pattern), that you can use in your program like this:
throw CustomException.FromJson(variable);
That way you followed best practice and can parse your json inside the exception class.