Re: [Py4j-users] way to access java exceptions in python
Status: Beta
Brought to you by:
barthe
From: Barthelemy D. <ba...@us...> - 2011-01-21 15:16:18
|
> Initially I was thinking about accessing it directly as and attribute > which seems a bit more pythonic. > But yes. That's the kind of way of doing it I had in mind. I don't see a straightforward way to merge a Python Exception and a Java Exception together like Py4J does for the collection classes, because you often explicitly mention the class name when catching an exception. For example, it would not be easy to support: try: javaObject.doSomething() except NullPointerException: ... To make this work, this kind of code would be required before catching the exception: NUllPointerException = gateway.jvm.NullPointerException._asPythonClass() Another alternative is to convert any java.lang.Throwable object to Py4JJavaError and to do something like: try: javaObject.doSomething() except Py4JJavaError as e: e.printStackTrace() I think this is what you had in mind, but I am reluctant to collapse the whole Java exception hierarchy into a single Python class and to merge Python and Java exceptions together (e.g., Python exceptions often have a args and with_traceback members, which would hide similar members in a Java exception). I'll have to think about this more. Barthélémy On 01/21/2011 09:29 AM, Jakub Gustak wrote: >> Hi, >> >> I agree with you, exceptions in some Java Library/Framework carry >> important information that should be available on the Python side. >> >> Py4J could wrap the Java Exception in the Py4JJavaError exception, a new >> class extending Py4JError. >> >> Something like: >> >> try: >> ... >> except Py4JJavaError as e: >> javaException = e.getJavaException() > > Initially I was thinking about accessing it directly as and attribute > which seems a bit more pythonic. > But yes. That's the kind of way of doing it I had in mind. > >> That would allow clients to differentiate between catching Java >> exceptions and catching Py4J exceptions (e.g., misuse of the library, >> network error, etc.). Clients could still use the catch all: >> >> except Py4JError: >> >> to catch any exception raised while using Py4J, Java exceptions included. >> >> I just need to make sure that there is no Python black magic that >> prevents the Python exception from being garbage collected. > > I wouldn't expect that. > > I assume (guessed without looking at the code actually) that as long > as python keeps a refernce to java object, java keeps it's reference > as well, and if python object get's garbage collected then java side > is notified so it can collect it's garbage too. It that right? > >> Would that work with your use case? > > In short - yes. > >> >> Barthélémy >> >> On 01/21/2011 06:42 AM, Jakub Gustak wrote: >>> I would like to propose a enhancement to a py4j. >>> >>> Right now the only way to get any info about java side exception is to >>> parse a error message string. >>> It would be quite convenient to be able to access the original java >>> exception object. >>> >>> As far as I understand the code it would require ReturnObject which >>> would be a hybrid of error and reference. >>> Is there any java side limitation than can make it hard to achieve? >>> >>> Best regards, >>> Jakub > > ------------------------------------------------------------------------------ > Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)! > Finally, a world-class log management solution at an even better price-free! > Download using promo code Free_Logger_4_Dev2Dev. Offer expires > February 28th, so secure your free ArcSight Logger TODAY! > http://p.sf.net/sfu/arcsight-sfd2d > _______________________________________________ > Py4j-users mailing list > Py4...@li... > https://lists.sourceforge.net/lists/listinfo/py4j-users |