コード例 #1
0
        public static void DoThrowWithInner()
        {
            using (Py.GIL())
            {
                // create a TypeError
                dynamic builtins   = Py.Import("builtins");
                var     pyerrFirst = new PythonException(new PyType(builtins.TypeError), value: null, traceback: null, "Type error, the first", innerException: null);

                // Create an ArgumentException, but as a python exception, with the previous type error as the inner exception
                var argExc      = new ArgumentException("Bogus bad parameter", pyerrFirst);
                var argExcPyObj = argExc.ToPython();
                var pyArgExc    = new PythonException(argExcPyObj.GetPythonType(), value: null, traceback: null, argExc.Message, innerException: argExc.InnerException);
                // This object must be disposed explicitly or else we get a false-positive leak.
                argExcPyObj.Dispose();

                // Then throw a TypeError with the ArgumentException-as-python-error exception as inner.
                var pyerrSecond = new PythonException(new PyType(builtins.TypeError), value: null, traceback: null, "Type error, the second", innerException: pyArgExc);
                throw pyerrSecond;
            }
        }