Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unity – How can I throw except out in coroutine "yield return"

IEnumerator CoTest()
{
  yield return CoTest2();

  Debug.Log("Done");
}

If CoTest2 had error , I want to stop exactly line I called ( yield return CoTest2(); )
before Debug.Log("Done");

Is there any way to do this ? or better way

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just surround your code with a "try/catch" block (see "Exception Handling" for further details). The further execution of your code will be immediately stopped when an exception occurs in CoTest2 and you will fall through to the catch block.

IEnumerator CoTest()
{
    try
    {
        yield return CoTest2();

        Debug.Log("Done");
    }
    catch( Exception ex )
    {
        // Handle the exception here
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading