Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Exceptions thrown from c# iterators (yield) cannot be caught #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sirwhinesalot opened this issue May 29, 2017 · 3 comments
Closed

Exceptions thrown from c# iterators (yield) cannot be caught #475

sirwhinesalot opened this issue May 29, 2017 · 3 comments
Labels
Milestone

Comments

@sirwhinesalot
Copy link

sirwhinesalot commented May 29, 2017

Environment

  • Pythonnet version: 2.3.0
  • Python version: 2.7.10
  • Operating System: Windows 7

Details

  • Describe what you were trying to get done.

Catch an exception thrown from a C# iterator (done with yield return) in a python script.

using System;
using System.Collections;
using System.Collections.Generic;
namespace Example
{
	public class Script 
	{
	    public static IEnumerable<int> Test()
	    {
	        for (var i = 0; i < 10; i++)
	        {
	            if (i == 5)
	                throw new Exception("Ooops!");
	            yield return i;
	        }
	    }
	}
}

compile:

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /t:library bad_yield.cs
import clr
clr.AddReference(...)
import System
import Example


try:
    for r in Example.Script.Test():
        print r
except System.Exception as e:
    print e.Message
except Exception as e:
    print e.message
except:
    print "Unfortunately this one doesn't work either"
  • If there was a crash, please include the traceback here.
Unhandled Exception: System.Exception: Ooops!
    at Example.Script.<Test>d__13.MoveNext() in ...
    at Python.Runtime.Iterator.tp_iternext(IntPtr ob)
@den-run-ai
Copy link
Contributor

@drlagos thanks for detailed report. I edited your response for full example.

@filmor filmor added the bug label Jun 6, 2017
@iantDev
Copy link

iantDev commented Jul 10, 2017

  • Python 3.6.1
  • Pythonnet 2.3
  • Windows 7

Similar issue with SqlDataReader:

   dbComm = SqlCommand(query, Conn)
    Conn.Open()        
    sqlReader = dbComm.ExecuteReader()
    sqlReader.Read() 
    sqlReader.Close()

I have tried the following as subsequent code before closing the reader:

while sqlReader.Read():
   pass

but same error.

Error message:

Unhandled Exception: System.InvalidOperationException: Inlvaid attempt to call FieldCount when reader is closed.
     at System.Data.SqlClient.SqlDataReader.get_FieldCount()
     at System.Data.Common.DbEnumerator.BuildSchemaInfo()
     at System.Data.Common.DbEnumerator.MoveNext()
     at Python.Runtime.Iterator.tp_iternext(IntPtr ob)

@filmor filmor added this to the 2.4.0 milestone Jul 11, 2017
filmor added a commit that referenced this issue Jun 26, 2018
Catching exceptions from iterator MoveNext(), additional testing (issue #475, original pull request #523)
@den-run-ai
Copy link
Contributor

fixed via #693 and #523

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants