dimanche 24 novembre 2019

Is there a design pattern or any typical solution for interrupting program execution without using exceptions?

I have method A called from another method of another class, which called from another method, etc. I have a condition in my A method and I want to do two things if it evaluates to false:

  1. print a certain message in a console
  2. interrupt program execution after that

The program must print an error message and then exit. I can implement it using exceptions, but I shouldn't, because it's not an exceptional case. Another way to do what I want is by using return value of method A. But in this case, I should use the return value of a method, calling method A, and so on. All the methods from the sequence of calls should return a value and no one of them can be ignored. I will have to write plenty of

if (!func()) return false;

and will be forced to print messages immediately before the first 'return' (using exceptions it is possible to print their own messages from a place where they were caught).

Is there any solution for my problem? Maybe there's something absolutely wrong in the way I'm thinking...

Aucun commentaire:

Enregistrer un commentaire