Try / Catch in coroutines causes an internal compiler error

Hey everyone,

I’m experiencing an internal compiler error when including any try/catch in a coroutine, it seems to reproduce no matter what the case.
I managed to reproduce it by simply creating a new, completely blank unity project (using Unity 5.2.2f1, and VS2010), adding a script to the main camera and adding this method to the class:

public System.Collections.IEnumerator TestCoroutine() {
    try{}
    catch (System.Exception e){}
    yield break;
}

This causes the console to be flooded with text, but the two important lines (I think) are these:

Unhandled Exception: System.ArgumentException: Trying to emit a local from a different ILGenerator.

Internal compiler error at Assets/dawdwd.cs(11,43):: exception caught while emitting MethodBuilder [<TestCoroutine>c__Iterator0::MoveNext]

I’ve searched both google and these forums and while it seems that others are experiencing the same issue, I’ve found no solution. I apologize if the answer is actually out there and I couldn’t find it, but what I’m mainly wanting to know is: Is this a widespread issue or is it only me and a few others that are experiencing this problem?

Any help is greatly appreciated!

Hey. This page could be helpful.

1 Like

Duugu, that page you linked actually really helped me. By some miracle of god Tim Tregubov’s TTMonoBehaviour script manages to compile without an internal compiler error which means I can reverse engineer it to suit what I need! Thanks man, muchly appreciated.

Not actually try{} catch(Exception) but catch{Exception e}, trying to access exception in coroutine cause internal compiler error

This is the kind of bug that will get an immediate response if you file a bug report! If you just post on the forums about these things, the bug will still be around one year later!