Class PythonExceptions
At runtime such a Python class is a plain Python exception whose args hold the arguments
of its super().__init__(...) call. The generated Java class reads them back through these
helpers to call the matching Java super constructor, so the message, the cause and any other
state of the Java base class survive the crossing into Java.
- Since:
- 5.2.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable org.graalvm.polyglot.Valueargument(org.graalvm.polyglot.Value exception, int index) Positional argumentindexof the Python exception (exception.args[index]).static @Nullable StringargumentAsString(org.graalvm.polyglot.Value exception, int index) Positional argumentindexof the Python exception as a string: the value itself when it is a string, itsstr()otherwise, the way a Python exception message reads.static @Nullable StringasString(@Nullable org.graalvm.polyglot.Value value) str(value): the value itself when it is a string, its Pythonstr()otherwise.static voidattachCause(Throwable throwable, org.graalvm.polyglot.Value exception) Attach the Python cause (raise ... from cause, or the exception being handled when the exception was raised inside anexceptblock) to the generated Java exception when the cause is a Java exception and the Java exception has no cause yet.static @Nullable Throwabledeclared(org.graalvm.polyglot.PolyglotException exception, Class<?> generatedClass, Class<?>... declaredTypes) The exception a generated bridge method declaring checked exceptions rethrows for the exception of its Python call: a host exception of one of the declared types (raised in Python, or thrown by a Java method the Python code called), or the generated Java exception of a Python exception class extending one of the declared types.static booleanisTruffleStackTrace(@Nullable Throwable suppressed) Whether a suppressed exception is Truffle bookkeeping rather than an exception the application suppressed.static @Nullable Stringmessage(org.graalvm.polyglot.Value exception) The message of the Python exception:str(exception)when the exception carries arguments,nullotherwise (a Python exception raised without arguments has an empty message, a Java exception anullone).static org.graalvm.polyglot.ValueprimitiveArgument(org.graalvm.polyglot.Value exception, int index, String parameterType) Positional argumentindexof the Python exception for a primitive parameter of the Java super constructor, which cannot take a missing orNoneargument.static Throwable[]suppressed(Throwable throwable) The exceptions the application suppressed on a throwable that crossed Python.
-
Method Details
-
isTruffleStackTrace
Whether a suppressed exception is Truffle bookkeeping rather than an exception the application suppressed.A Java exception that Python creates and raises is wrapped by Truffle, which shares the guest frames it unwinds through with the host throwable by attaching a
TruffleStackTrace.LazyStackTraceto it as a suppressed exception. That entry carries no message and no stack trace of its own, it is not an exception the application suppressed, and a serializer rendering the suppressed exceptions of an error response has nothing to write for it: the problem+json body of a Java problem raised from Python failed withNo serializable introspection present for type LazyStackTraceand the client saw a 500.- Parameters:
suppressed- A suppressed exception- Returns:
truewhen the entry is Truffle's guest stack trace- Since:
- 5.2.4
-
suppressed
The exceptions the application suppressed on a throwable that crossed Python.Throwable.getSuppressed()of a Java exception that crossed Python also reports Truffle's guest stack trace (seeisTruffleStackTrace(Throwable)). The entry cannot be taken off the throwable:Throwable.addSuppressed(Throwable)andThrowable.getSuppressed()are final, there is no removal, and the field behind them is injava.base/java.lang, which is not open. This is the view of the suppressed exceptions without it, for code that reports or serializes them; the genuine entries keep their order.- Parameters:
throwable- The throwable- Returns:
- The suppressed exceptions of the application
- Since:
- 5.2.4
-
argument
public static @Nullable org.graalvm.polyglot.Value argument(org.graalvm.polyglot.Value exception, int index) Positional argumentindexof the Python exception (exception.args[index]).- Parameters:
exception- The Python exceptionindex- The argument index- Returns:
- The argument, or
nullwhen the exception has fewer arguments
-
primitiveArgument
public static org.graalvm.polyglot.Value primitiveArgument(org.graalvm.polyglot.Value exception, int index, String parameterType) Positional argumentindexof the Python exception for a primitive parameter of the Java super constructor, which cannot take a missing orNoneargument.- Parameters:
exception- The Python exceptionindex- The argument indexparameterType- The name of the primitive parameter type- Returns:
- The argument
- Throws:
IllegalArgumentException- When the exception has fewer arguments or the argument isNone
-
argumentAsString
Positional argumentindexof the Python exception as a string: the value itself when it is a string, itsstr()otherwise, the way a Python exception message reads.- Parameters:
exception- The Python exceptionindex- The argument index- Returns:
- The argument, or
nullwhen the exception has fewer arguments or the argument isNone
-
message
The message of the Python exception:str(exception)when the exception carries arguments,nullotherwise (a Python exception raised without arguments has an empty message, a Java exception anullone).- Parameters:
exception- The Python exception- Returns:
- The message, or
null
-
declared
public static @Nullable Throwable declared(org.graalvm.polyglot.PolyglotException exception, Class<?> generatedClass, Class<?>... declaredTypes) The exception a generated bridge method declaring checked exceptions rethrows for the exception of its Python call: a host exception of one of the declared types (raised in Python, or thrown by a Java method the Python code called), or the generated Java exception of a Python exception class extending one of the declared types. Any other Python exception is reported as thePolyglotExceptionitself.- Parameters:
exception- The exception of the Python callgeneratedClass- The generated class of the bridge method, whose class loader loads the generated exceptionsdeclaredTypes- The checked exception types the bridge method declares- Returns:
- The exception to rethrow, or
nullwhen the Python exception is none of them
-
attachCause
Attach the Python cause (raise ... from cause, or the exception being handled when the exception was raised inside anexceptblock) to the generated Java exception when the cause is a Java exception and the Java exception has no cause yet.- Parameters:
throwable- The generated Java exceptionexception- The Python exception it was created from
-
asString
str(value): the value itself when it is a string, its Pythonstr()otherwise.- Parameters:
value- The value- Returns:
- The string, or
nullforNone
-