use try catch in coroutine unity

using System;
using System.Collections;
 
using UnityEngine;
 
/// <summary>
/// Utility functions to handle exceptions thrown from coroutine and iterator functions
/// http://JacksonDunstan.com/articles/3718
/// </summary>
public static class CoroutineUtils
{
	/// <summary>
	/// Start a coroutine that might throw an exception. Call the callback with the exception if it
	/// does or null if it finishes without throwing an exception.
	/// </summary>
	/// <param name="monoBehaviour">MonoBehaviour to start the coroutine on</param>
	/// <param name="enumerator">Iterator function to run as the coroutine</param>
	/// <param name="done">Callback to call when the coroutine has thrown an exception or finished.
	/// The thrown exception or null is passed as the parameter.</param>
	/// <returns>The started coroutine</returns>
	public static Coroutine StartThrowingCoroutine(
		this MonoBehaviour monoBehaviour,
		IEnumerator enumerator,
		Action<Exception> done
	)
	{
		return monoBehaviour.StartCoroutine(RunThrowingIterator(enumerator, done));
	}
 
	/// <summary>
	/// Run an iterator function that might throw an exception. Call the callback with the exception
	/// if it does or null if it finishes without throwing an exception.
	/// </summary>
	/// <param name="enumerator">Iterator function to run</param>
	/// <param name="done">Callback to call when the iterator has thrown an exception or finished.
	/// The thrown exception or null is passed as the parameter.</param>
	/// <returns>An enumerator that runs the given enumerator</returns>
	public static IEnumerator RunThrowingIterator(
		IEnumerator enumerator,
		Action<Exception> done
	)
	{
		while (true)
		{
			object current;
			try
			{
				if (enumerator.MoveNext() == false)
				{
					break;
				}
				current = enumerator.Current;
			}
			catch (Exception ex)
			{
				done(ex);
				yield break;
			}
			yield return current;
		}
		done(null);
	}
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source