The ThrowException method essentially boils down to the following
Emit(OpCodes.NewObj, ...); Emit(OpCodes.Throw);
The key here is to replace the first Emit calls Emit set of IL statements needed to instantiate your custom exception. Then add Emit(OpCodes.Throw)
for instance
class MyException : Exception { public MyException(int p1) {} } var ctor = typeof(MyException).GetConstructor(new Type[] {typeof(int)}); var gen = builder.GetILGenerator(); gen.Emit(OpCodes.Ldc_I4, 42); gen.Emit(OpCodes.NewObj, ctor); gen.Emit(OpCodes.Throw);
source share