void methodA() {
if (1 == 1) {
checkIfExitMethodA();
doSomething();
} else {
print('nothing');
}
}
void checkIfExitMethodA() => print('check whether to exit the parent function');
Is it possible to implement checkIfExitMethodA()
method which contains few conditions to check and methods to exucate, and once it meets it will exit methodA()
entirely?
It can be done by simply putting a return
but I would like to exit by assessing checkIfExitMethodA()
before doSomething
.
2
Answers
I guess the only way to do it is to throw exception.
The obvious way would be a return value to be checked:
There is no "normal" way to tell the caller to exit their method. There is the "exceptional" way mentioned in another answer though, maybe that suits better you:
Please note that this will not "exit method A". It will exit all methods until you implement a catch block.