views:

75

answers:

1

I'm writing .NET On-the-Fly compiler for CLR scripting. And have a dilemma: is it better to throw an exception on build fail or not?

So what is the best-practice point of view, which approach is more suitable?

try
{
  compiler.Compile(); // do not throws an exception only if build succeed
}
catch(CompilerException ex)
{
  string err = ex.Message;
}

or

compiler.Compile(); // throws an exception only in case of crash, etc
if(!compiler.BuildSucceed)
{
  string err = compiler.Output.ToString();
}
+5  A: 

Exceptions should be used for exceptional conditions. I would say that a failed build isn't exceptional, so you should have your compiler's interface return a value indicating success or failure (e.g. a bool or an enum), and provide an interface for returning a list of errors. This can be as simple as returning a string of all the errors concatenated together, or it can be something like you passing it an output stream beforehand, and it logs each error to that stream as it comes across them.

Adam Rosenfield
I agree - a failed build is a perfectly valid outcome for a compiler, not an error scenario, and so should be catered for by the method itself rather than via exceptions. +1.
sgreeve