This is a language feature that was intentionally omitted from C#intentionally omitted from C#. Quite simply, it's entirely possible for an IUserRepository.GetByID to fail for some other reason entirely than the user not being found, so you don't want to require a specific error when such cannot happen. You have two options if for whatever reason you want to enforce this behavior:
- Define the
Userclass so that it itself throws the exception if it's improperly initalized. - Write unit tests for
IUserRepositorythat explicitly test for this behavior.
Note that neither of those options is "include it in the documentation." Ideally, you should do that anyway, especially as documentation is where you can explain why you want to enforce a particular error type.