question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return the actual Java Exception object on Exception?

See original GitHub issue

Hi!

Just wondering, I couldn’t seem to be able to get the actual Java Exception object returned as an Exception? Is this by design, or is this something that could be part of future features?

For example, it would be nice to be able to do:

MyException = jnius.autoclass("myjavaprogram.exceptions.MyException")
MyClass = jnius.autoclass("myjavaprogram.MyClass")
try:
  MyClass.doSomethingThatThrowsAnException()
except MyException:
  # Do something
  pass
except JavaException:
  # Do something else
  pass

As an addendum, MyException would also have to inherit from jnius.JavaException

<bountysource-plugin> --- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/30741053-return-the-actual-java-exception-object-on-exception?utm_campaign=plugin&utm_content=tracker%2F77133&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F77133&utm_medium=issues&utm_source=github). </bountysource-plugin>

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jcrotingercommented, May 8, 2020

Got it - thanks! It would be nice if the message included the method name, since it must be known.

I was able to print the stack trace with

      print("".join(traceback.TracebackException.from_exception(e).format()))

and sorted it out.

(The confusion was that the code had something like foo.bar(a.getX(), a.getY(), ...) where foo and a are Java objects. I assumed the problem was somewhere down in foo.bar and only later realized that it wasn’t getting that far. Debugging this interlanguage stuff can be a headache so more info in exceptions helps!)

0reactions
tshirtmancommented, May 7, 2020

i would assume there https://github.com/kivy/pyjnius/blob/master/jnius/jnius_export_class.pxi#L831-L836

which would mean the error didn’t happen in java, it happened in python before the call to the java function, so there is no java stacktrace to report.

edit: the latter part of the message (after “got {one}”) was added in the latest version of pyjnius, released last weekend, which also solved some bugs in the lookup of methods, you might want to upgrade and try with that version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - How to return a list of objects along with Exception
In this case, the method returns the list of valid student information. But in case of an Exception, I'm not able to find...
Read more >
Return Statement in Try-Catch - Java Exception Handling ...
In a try-catch-finally block that has return statements, only the value from the finally block will be returned. When returning reference types, be...
Read more >
Java Exception Handling: How to Specify and ... - Stackify
When a method throws an exception object, the runtime searches the call stack for a piece of code that handles it. I will...
Read more >
Exception Handling in Java - DigitalOcean
Java creates an exception object when an error occurs while executing a statement. The exception object contains a lot of debugging information ...
Read more >
Best Practice: Catching and re-throwing Java Exceptions - IBM
Customers often have general catch blocks in Servlets, MDBs, EJBs and other core components where they catch all un-handled exceptions and re- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found