using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Runtime.Serialization;
using System.Security.Permissions;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public class InternalException : Exception, ISerializable
protected InternalException(SerializationInfo info, StreamingContext context)
public InternalException()
public InternalException(string message)
public InternalException(string message, Exception innerException)
: base(message, innerException)
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
base.GetObjectData(info, context);
public static void Test()
throw new InternalException("some message");
catch (InternalException ex)
var json = JsonConvert.SerializeObject(ex, Formatting.Indented);
var ex2 = JsonConvert.DeserializeObject<InternalException>(json);
var json2 = JsonConvert.SerializeObject(ex2, Formatting.Indented);
Assert.That(JToken.DeepEquals(JToken.Parse(json), JToken.Parse(json2)));
Assert.AreEqual(ex.Message, ex2.Message);
Assert.AreNotEqual(ex.Message, new InternalException(), ex.Message);
public static void Main()
Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Namespace, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");