Skip to content

Instantly share code, notes, and snippets.

@dmorrison42
Created March 14, 2017 15:39
Show Gist options
  • Save dmorrison42/c798c0d295e2a4622cd3fb0912d258a9 to your computer and use it in GitHub Desktop.
Save dmorrison42/c798c0d295e2a4622cd3fb0912d258a9 to your computer and use it in GitHub Desktop.
Error handling
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
var magicObject = new Device();
AppDomain.CurrentDomain.FirstChanceException +=
(object source, FirstChanceExceptionEventArgs e) =>
{
var target = e.Exception.TargetSite;
if (target == null) return;
if (target.DeclaringType != typeof(Device)) return;
Console.WriteLine("FirstChanceException event raised in {0}: {1}",
AppDomain.CurrentDomain.FriendlyName, e.Exception.Message);
};
propertyGrid1.SelectedObject = magicObject;
}
}
public class Device {
public string Info
{
set { throw new ArgumentException(); }
get { return ""; }
}
}
internal static class Magic {
public static object HandlePropertyExceptions(object obj) {
dynamic outObj = new ExpandoObject();
outObj.get_First = new Func<string>(() => "e");
outObj.set_First = new Action<string>(p => { });
outObj.Second = "blue";
return outObj;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment