Steve88
Steve88

Reputation: 2416

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!

Upvotes: 2

Views: 138

Answers (2)

Matt Allwood
Matt Allwood

Reputation: 1438

Alternatively raise your exceptions in FunctionSomethingX, though then there's no need for them to return boolean 'success' values.

The benefit of doing it this way is that FunctionSomethingX likely has an understanding as to why it failed and hence return a more descriptive error message.

The downside of is that all other places that call FunctionSomethingX will need to handle the exceptions raised by them.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612964

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

Upvotes: 7

Related Questions