The calling parent method:
public void Script()
{
string str = "some text";
var someText = !CheckStr(str);
if (someText)
{
return;
}
str = "some other text";
var someOtherText = !CheckStr(str);
if (someOtherText)
{
return;
}
str = "some other other text";
var someOtherOtherText = !CheckStr(str);
if (someOtherOtherText )
{
return;
}
// ...continue
}
The method that is being called:
public bool CheckStr(string str)
{
if (str == "error")
{
return false;
}
// ...additional checks
return true;
}
Is there a way that if CheckStr()
returns false
that it also stops the Scripts()
method from further executing?
Right now I have to do a bunch of if
checks in the Script()
method, it feels a bit like I'm repeating myself with the if statement
doing the same thing after each CheckStr()
. It would be better if the CheckStr()
halts te execution of Script()
. Is something like this possible?
Yes, you can throw a exception.
It will interrupt calling "parents" (and their "parents") until a execption-handler is somewhere (try-catch block). Just google for exception-throwing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.