I am having a hard time when it comes to good design.
I am unsure if I should let a later method throw an exception or if I should throw it myself.
Thing is:
The type of the Exception changes based on the method being called. In the later method it might be an InvalidOperation but in the current method an ArgumentException may better fit.
Now should I really perform all the checks in order to throw the correct type of exception or should I let the deeper method throw the incorrect type of exception?
Consider this:
private bool _canWaste = false;
/// <summary>
/// Runs around and wastes something.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Thrown when <paramref name="CanPower"/> is false.
/// </exception>
public void Run(bool CanWaste) {
_canWaste = CanWaste;
// <- Should I check if we can waste and throw an ArgumentException here or
// throw the 'wrong' type later in Waste?
Waste();
}
/// <summary>
/// Wastes something.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Thrown when we cannot waste.
/// </exception>
private void Waste() {
if(!_canWaste) throw new InvalidOperationException("We cannot waste.");
/* ... */
}
This is a simple example (that does not make sense) where it would be easly done.
But of course, there are methods where the evaluation is much harder.
Another option would be to catch the exception and throw a new of the correct type.
But I guess that would hit performance and register some uncool stuff in the ouput.
Do not throw or let the code throw incorrect type exception, that doesn't help any one. If you get an invalid argument throw
InvalidArgumentException
, otherwise let the exception bubble up to the caller from the library.From the description, it sounds like that you are trying to control the execution flow through exceptions. Do not do that. Exceptions are "Exceptions", use them when there are exceptional scenarios that you can't avoid.
There is no point in catching and re-throwing that exception, unless you want to reset the stack trace yourself.