|

How to use try/catch inside of a coroutine

Ever wanted to do yield return null inside of a try/catch block but your Compiler/Editor says NOPE!?

I consider this a bit of a cludge, but it does the job:

private bool result = true;
private void SomeMethod() {
    while (result) {
        yield return StartCoroutine(DoSomethingCo());
    }
}
private void DoSomethingCo() {
    try {
        fileStream.Read(...);
    catch (Exception ex) {
        result = false;
    }
}

Similar Posts

Leave a Reply