But if we do that, how are we going to raise an exception if the algorithm selected is not SHA1 in the case of HOTP and not SHA1, SHA256 or SHA512 in the case of TOTP?

While it isn't likely that using SHA256 and SHA512 breaks HOTP given how similar the two algorithms are, it's not explicitly defined in the RFC after all.