What would be the most correct thing to do in a public API that uses Type.GetType(typeName) under the hood?
//inside a message deserializer of a framework...
....
//1. throw TypeLoadException
var type = Type.GetType(typeName,true);
//2. or..
var type = Type.GetType(typeName,false);
if (type == null)
throw new SomeMoreSpecificException("Could not find message type " + typeName +", deserialization failed");
//3. or
Type type;
try
{
type = Type.GetType(typeName,true);
}
catch(TypeLoadException x)
{
throw new SomeMoreSpecificException("some message",x);
}
Which of the above approaches would you consider the most helpful for the end user in this case?
I leaning towards the latter case here, as you get both the real TypeLoadException and some additional information specific for this very usecase. Or should we just consider the TypeLoadException itself to be enough for the end user?
Thoughts?
[edit] for more context, see https://github.com/akkadotnet/akka.net/issues/1279
In Akka.NET we can do "remote deployment" of actors. if the remote system receiving the deploy request does not know about the type that should be deployed, we need to notify this somehow. Only throwing TypeLoadException feels a bit cheap, as it does not pinpoint the problem to the remote deploy scenario.
There's not a right and wrong answer here. The trade off is having more details available in the thrown exception versus the overhead of having two exceptions throws (the original one and the custom one) instead of one.
The question is - what do you expect the user to do with the exception you're throwing? Does the "some message" you provide give more detail than the original exception? Or does it let the user do something different if it gets that specific exception type? If not, I'd just let the original exception bubble up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With