Search code examples
delphidelphi-xe

Working with Exceptions


Hy! I would like to ask for an advice in using exceptions. I want to simplify this...

if (functionSomething1) and (functionSomething2) and (functionSomething3) then
  Do Something Good
else
  Raise Exception(errormsg_functionSomething1IsFalse or errormsg_functionSomething2IsFalse or errormsg_functionSomething3IsFalse);

All my functions is returning a boolean value.

What is the best way to get the error msg one by one?

For example... If functionSomething1 is False then I should get errormsg_functionSomething1IsFalse

If functionSomething2 is False then I should get errormsg_functionSomething1IsFalse etc...

I think If-then-else is not a good solution.

Case or try except? What do you think?

Thanks for the answers!


Solution

  • Like this:

    if not functionSomething1 then
      raise EMyException.Create(errormsg_functionSomething1IsFalse);
    if not functionSomething2 then
      raise EMyException.Create(errormsg_functionSomething2IsFalse);
    if not functionSomething3 then
      raise EMyException.Create(errormsg_functionSomething3IsFalse);
    
    // do something good