You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@reef.apache.org by Markus Weimer <ma...@weimo.de> on 2015/04/03 17:40:17 UTC

On Exceptions.Throw()

Hi,

In C#, we use `Exceptions.Throw()` in place of the `throw` keyword. I have
found myself in situations where that leads to unfortunate code: Consider a
method that tries to load a file and return its contents but will throw a
`FileNotFoundException` when it can't:

string Load(string name)
{
  If(File.Exists(name))
  {
    return ...
  }
  else
  {
    Exceptions.Throw(...)
    return null;
  }
}

Note the extra `return null`: This is needed because the compiler can't
know that the control flow ends with `Exceptions.Throw()`. This seems like
a bad idea, as I won't be smart enough to remember this when editing this
code a month from now. Is there a better pattern in these cases?

Or, more broadly speaking: What is the benefit of `Exceptions.Throw()` over
the `throw` keyword that make this worthwhile?

Thanks,

Markus