The "stream = args.Result" line in asyncReader is the culprit. The Result property will throw if the read operation failed. You could move that line inside the try block, so the exception is caught. Or alternatively, use the args.Exception property to determine if there was an error in the read.
- John -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Benjamin West Sent: Wednesday, June 27, 2007 5:22 PM To: Discussion of IronPython Subject: [IronPython] catching WebException from OpenReadAsync Is this a bug or am I doing something wrong? $ cat webclient.py import clr import System from System import Uri from System.Net import WebClient, WebException class Foo: def __init__(self, uri): self.setup(uri) def setup(self, uri): self.uri = Uri(uri) self.wc = WebClient() self.wc.OpenReadCompleted += self.asyncReader try: self.wc.OpenReadAsync(self.uri) print "fetching.... please wait." except (Exception, WebException), e: print "got exception: %s" % e def asyncReader(self, sender, args): stream = args.Result try: print str(stream) print "Success." except (Exception, WebException), e: print "got exception: %s" % e $ ipy IronPython 1.1 (1.1) on .NET 2.0.50727.42 Copyright (c) Microsoft Corporation. All rights reserved. >>> from webclient import * >>> f = Foo('http://www.google.com/bad/url') fetching.... please wait. >>> Unhandled exception: Traceback (most recent call last): File System, line unknown, in OpenReadAsyncCallback File System, line unknown, in GetWebResponse File System, line unknown, in EndGetResponse File mscorlib, line unknown, in PerformWaitCallback File mscorlib, line unknown, in Run File mscorlib, line unknown, in WaitCallback_Context File System, line unknown, in OpenReadOperationCompleted File System, line unknown, in OnOpenReadCompleted File System, line unknown, in Invoke File , line 0, in System.Void(Object, OpenReadCompletedEventArgs)##73 File c:\Program Files\Internet Explorer\webclient.py, line 21, in asyncReader File , line 0, in get_Result##76 File System, line unknown, in get_Result File System, line unknown, in RaiseExceptionIfNecessary StandardError: An exception occurred during the operation, making the result invalid. Check InnerException for exception details. Unhandled Exception: System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid. Check InnerException for exception details. ---> System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) at System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult result) --- End of inner exception stack trace --- at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() at System.Net.OpenReadCompletedEventArgs.get_Result() at get_Result##76(Object ) at IronPython.Runtime.Calls.CallTarget1.Invoke(Object arg0) at IronPython.Runtime.Calls.FastCallable1.CallInstance(ICallerContext context, Object arg0) at IronPython.Runtime.Calls.FastCallable1.CallInstance(ICallerContext context, Object instance, Object[] args) at IronPython.Runtime.Types.ReflectedGetterSetter.CallGetter(Object instance, Object[] args) at IronPython.Runtime.Types.ReflectedProperty.GetAttribute(Object instance, Object context) at IronPython.Runtime.Operations.Ops.GetDescriptor(Object o, Object instance, Object context) at IronPython.Runtime.Types.DynamicType.UncheckedGetDescriptor(Object o, Object instance, Object context) at IronPython.Runtime.Types.DynamicType.TryBaseGetAttr(ICallerContext context, Object self, SymbolId name, Object& ret) at IronPython.Runtime.Types.ReflectedType.TryGetAttr(ICallerContext context, Object self, SymbolId name, Object& ret) at IronPython.Runtime.Types.DynamicType.GetAttr(ICallerContext context, Object self, SymbolId name) at IronPython.Runtime.Operations.Ops.GetAttr(ICallerContext context, Object o, SymbolId name) at webclient.asyncReader$f146(FunctionEnvironment8Dictionary $env, Object self, Object sender, Object args) in c:\Program Files\Internet Explorer\webclient.py:line 21 at IronPython.Runtime.Calls.Function3.Call(ICallerContext context, Object arg0, Object arg1, Object arg2) at IronPython.Runtime.Calls.Function3.Call(ICallerContext context, Object[] args) at IronPython.Runtime.Calls.PythonFunction.CallInstance(ICallerContext context, Object instance, Object[] args) at IronPython.Runtime.Calls.Method.Call(ICallerContext context, Object[] args) at IronPython.Runtime.Operations.Ops.Call(Object func, Object[] args) at IronPython.Runtime.Types.ReflectedEvent.EventDispatcher.Call(Object[] args) at IronPython.Runtime.Operations.Ops.Call(Object func, Object[] args) at IronPython.Runtime.Operations.Ops.Call(Object func, Object arg0, Object arg1) at System.Void(Object, OpenReadCompletedEventArgs)##73(Object , Object , OpenReadCompletedEventArgs ) at System.Net.OpenReadCompletedEventHandler.Invoke(Object sender, OpenReadCompletedEventArgs e) at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) at System.Net.WebClient.OpenReadOperationCompleted(Object arg) at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state) _______________________________________________ users mailing list [email protected] http://lists.ironpython.com/listinfo.cgi/users-ironpython.com _______________________________________________ users mailing list [email protected] http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
