3

In C#.NET how can I create a new exception class and throw it at Runtime. I need to generate the exception class name during runtime based on a string I receieve as input. It seems I should use Reflection.emit, but I don't know how to do it.

cubitouch
  • 1,929
  • 15
  • 28
webdude
  • 35
  • 4
  • 10
    You'll need to give us more context. This sounds like a bad idea, to be honest - what problem are you trying to solve? – Jon Skeet Jan 21 '14 at 08:15
  • 4
    If you generate the exception type at runtime, how do you expect someone to be able to catch it? Unless they catch one of the base exception types, in which case, what use is your exception class? – Lukazoid Jan 21 '14 at 08:17
  • @Lukazoid I agree that it seems a bit "unusable" but the question seems interessting to me to solve. – cubitouch Jan 21 '14 at 08:19
  • Do not do it. Writing `your own exception class is a code smell` . – now he who must not be named. Jan 21 '14 at 08:20
  • 4
    @nowhewhomustnotbenamed.: I wouldn't say that. I'd say that doing it *at execution time*, based on a string, is a code smell... but there are perfectly good reasons for declaring your own exception types. – Jon Skeet Jan 21 '14 at 08:24
  • @JonSkeet: **Got it**. `doing it at execution time say based on a string is a code smell`. Exact. Thanks. – now he who must not be named. Jan 21 '14 at 08:27
  • 1
    I don't need to catch it, I need it for testing purposes (I want to get something out of a closed system). It will not be used in a real program (for users let's say). Otherwise I realize it's a really bad practice to do. – webdude Jan 21 '14 at 08:31
  • @user3218247 Still do you need an exception class with a custom name for that? Isn't the exception message enough? – svick Jan 21 '14 at 10:21
  • No, it returns only the Exception name without the message. – webdude Jan 21 '14 at 12:34

2 Answers2

3

While I do not understand the purpose of creating an exception type using reflection emit, creating an exception type is no different than creating any other type:

// Build an assembly ...
var appDomain = Thread.GetDomain();
var assemblyName = new AssemblyName("MyAssembly");
var assemblyBuilder = appDomain.DefineDynamicAssembly(
  assemblyName,
  AssemblyBuilderAccess.Run
);

// ... with a module ...
var moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule");

// ... containing a class.
var typeBuilder = moduleBuilder.DefineType(
  "MyException",
  TypeAttributes.Class,     // A class ...
  typeof(Exception)         // ... deriving from Exception
);
var exceptionType = typeBuilder.CreateType();

// Create and throw exception.
var exception = (Exception) Activator.CreateInstance(exceptionType);
throw exception;
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
0

I think you can look into this article for exception : http://blog.gurock.com/articles/creating-custom-exceptions-in-dotnet/

And to this thread to solve the definition issue at runtime : Creating a class for an interface at runtime, in C#

Community
  • 1
  • 1
cubitouch
  • 1,929
  • 15
  • 28
  • Answers shouldn't just contain links, they should contain the necessary information on their own. – svick Jan 21 '14 at 10:24