Do not swallow exception on NameError within constant.

Fixes https://github.com/rails/rails/issues/9933.
@matthewd @senny I made a new commit to demonstrate how we can use `safe_constantize` for the error handling. However, we'll have to handle the `NoMethodError` exception in `safe_constantize` which I'm not 100% sure if it should be the responsibility of the method.