Package org.graalvm.polyglot
Class PolyglotException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
org.graalvm.polyglot.PolyglotException
- All Implemented Interfaces:
Serializable
A polyglot exception represents errors that contain Graal guest languages on the stack trace. In
addition to the Java stack trace it also returns a
polyglot stack
trace
. Methods like printStackTrace()
are implemented such that host and guest language
stack traces are printed nicely.
A polyglot exception may have the following properties:
Guest Exception
: Istrue
if the exception was raised in guest language code.Host Exception
: Istrue
if this exception was raised in host runtime code. This may happen if the polyglot runtime host runtime methods that throw an exception. The original host exception can be accessed usingasHostException()
.Cancelled
: Istrue
if the execution got cancelled. The execution may be cancelled whenclosing
a context, by a guest language intrinsic or by a tool, like the debugger.Exit
: Istrue
if the execution exited. The guest language triggers exit events if the guest language code request to exit the VM. The exit status can be accessed usinggetExitStatus()
.Syntax Error
: Istrue
if the error represents a syntax error. For syntax errors alocation
may be available.Incomplete Source
: Istrue
if this returns asyntax error
that indicates that the source is incomplete.Resource exhausted
: Istrue
if a resource limit e.g. the maximum memory was exhausted.Internal Error
: Istrue
if an internal implementation error occurred in the polyglot runtime, the guest language or an instrument. It is not recommended to show such errors to the user in production. Please consider filing issues for internal implementation errors.
- Since:
- 19.0
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionfinal class
Represents a polyglot stack frame originating from a guest language or the host language Java. -
Method Summary
Modifier and TypeMethodDescriptionReturns the original Java host exception that caused this exception.boolean
Unsupported,PolyglotException
instances are not writable therefore filling the stack trace has no effect for them.int
Returns the exit status if this exception indicates that the application wasexited
.Returns an additional guest language object.Gets a user readable message for the polyglot exception.Provides programmatic access to the polyglot stack trace information printed byprintStackTrace()
.Gets a guest language source location of this error ornull
if no source location is available for this exception.Gets stack trace elements for Java and guest languages.int
hashCode()
boolean
Returnstrue
if the execution was cancelled.boolean
isExit()
Returnstrue
if this exception is caused by an attempt of a guest language program to exit the application.boolean
Returnstrue
if this exception originates from a Graal guest language.boolean
Returnstrue
if this exception originates from the Java host language.boolean
Returnstrue
if this exception indicates a syntax error that is indicating that the syntax is incomplete.boolean
Returnstrue
if this exception was caused by an internal implementation error.boolean
Returnstrue
if the current application thread was interrupted by anInterruptedException
, or by callingContext.interrupt(Duration)
from the host.boolean
Returnstrue
if this exception indicates that a resource limit was exceeded, elsefalse
.boolean
Returnstrue
if this exception indicates a parser or syntax error.void
Prints host and guest language stack frames to the standarderror output
.void
Prints host and guest language stack frames to specified print stream.void
Prints host and guest language stack frames to specified print writer.void
setStackTrace
(StackTraceElement[] stackTrace) Unsupported,PolyglotException
instances are not writable therefore setting the stack trace has no effect for them.Methods inherited from class java.lang.Throwable
addSuppressed, getCause, getLocalizedMessage, getSuppressed, initCause, toString
-
Method Details
-
printStackTrace
public void printStackTrace()Prints host and guest language stack frames to the standarderror output
.- Overrides:
printStackTrace
in classThrowable
- Since:
- 19.0
-
printStackTrace
Prints host and guest language stack frames to specified print stream.- Overrides:
printStackTrace
in classThrowable
- Since:
- 19.0
-
printStackTrace
Prints host and guest language stack frames to specified print writer.- Overrides:
printStackTrace
in classThrowable
- Since:
- 19.0
-
fillInStackTrace
Unsupported,PolyglotException
instances are not writable therefore filling the stack trace has no effect for them.- Overrides:
fillInStackTrace
in classThrowable
- Since:
- 19.0
-
getStackTrace
Gets stack trace elements for Java and guest languages. For polyglot exceptions it recommended to usegetPolyglotStackTrace()
as the guest language stack elements do not always fit the Java format for stack trace elements.- Overrides:
getStackTrace
in classThrowable
- Since:
- 19.0
-
getMessage
Gets a user readable message for the polyglot exception. In case the exception isinternal
then the original java class name is included in the message. The message may returnnull
if no message is available.- Overrides:
getMessage
in classThrowable
- Since:
- 19.0
-
getSourceLocation
Gets a guest language source location of this error ornull
if no source location is available for this exception.- Since:
- 19.0
-
equals
-
hashCode
public int hashCode() -
setStackTrace
Unsupported,PolyglotException
instances are not writable therefore setting the stack trace has no effect for them.- Overrides:
setStackTrace
in classThrowable
- Since:
- 19.0
-
getPolyglotStackTrace
Provides programmatic access to the polyglot stack trace information printed byprintStackTrace()
. Returns an array of stack trace elements, each representing one stack frame. The zeroth element of the array (assuming the array's length is non-zero) represents the top of the stack, which is the last invocation in the sequence. Typically, this is the point at which this throwable was created and thrown. The last element of the array (assuming the array's length is non-zero) represents the bottom of the stack, which is the first method invocation in the sequence.- Since:
- 19.0
- See Also:
-
isHostException
public boolean isHostException()Returnstrue
if this exception originates from the Java host language. In such a case the firststack frame
returns ahost frame
as zeroth element.- Since:
- 19.0
-
isGuestException
public boolean isGuestException()Returnstrue
if this exception originates from a Graal guest language. In such a case the firststack frame
returns aguest frame
as zeroth element.- Since:
- 19.0
-
asHostException
Returns the original Java host exception that caused this exception. The original host exception contains a stack trace that is hardly interpretable by users as it contains details of the language implementation. The polyglot exception provides information for user-friendly error reporting with thepolyglot stack trace
.- Throws:
UnsupportedOperationException
- if this exception is not a host exception. CallisHostException()
to ensure its originating from the host language.- Since:
- 19.0
-
isInternalError
public boolean isInternalError()Returnstrue
if this exception was caused by an internal implementation error. These errors should be reported as bugs if observed. Internal error messages are typically hard to understand for guest language programmers and might contain implementation specific details that allows guest language implementers to debug the problem.- Since:
- 19.0
-
isResourceExhausted
public boolean isResourceExhausted()Returnstrue
if this exception indicates that a resource limit was exceeded, elsefalse
. Resource limit exceeded errors may be raised for the following reasons:- The host runtime run out of memory or stack space. For example if host runtime throws
OutOfMemoryError
orStackOverflowError
, then they will be translated to aPolyglotException
that returntrue
forisResourceExhausted()
. - A configured
resource limit
was exceeded. - A runtime specific per context resource limit was exceeded. Depending on the host runtime implementation additional options to restrict the resource usage of a context may be specified using options.
Resource limit exceptions may be originating from the
host
orguest
. Resource limit exceeded errors are neverinternal
, but may have caused the context to becancelled
such that it is no longer usable.- Since:
- 20.2
- The host runtime run out of memory or stack space. For example if host runtime throws
-
isCancelled
public boolean isCancelled()Returnstrue
if the execution was cancelled. The execution can be cancelled byclosing
a context, if an instrument such as a debugger decides to cancel the current execution or if aresource limit
was exceeded. The context that caused a cancel event becomes unusable, i.e. closed.- Since:
- 19.0
-
isInterrupted
public boolean isInterrupted()Returnstrue
if the current application thread was interrupted by anInterruptedException
, or by callingContext.interrupt(Duration)
from the host.- Since:
- 20.3
-
isExit
public boolean isExit()Returnstrue
if this exception is caused by an attempt of a guest language program to exit the application. The provided exit code can be accessed usinggetExitStatus()
. This can be the result of either the soft or the hard exit.- Since:
- 19.0
- See Also:
-
isSyntaxError
public boolean isSyntaxError()Returnstrue
if this exception indicates a parser or syntax error. In such a case #get- Since:
- 19.0
-
isIncompleteSource
public boolean isIncompleteSource()Returnstrue
if this exception indicates a syntax error that is indicating that the syntax is incomplete. This allows guest language programmers to find out if more code is expected from a given source. For example, an incomplete JavaScript program could look like this:function incompleteFunction(arg) {
A shell might react to this exception and prompt for additional source code, if this method returnstrue
.- Since:
- 19.0
-
getGuestObject
Returns an additional guest language object. Returnsnull
if no exception object is available.- Since:
- 19.0
-
getExitStatus
public int getExitStatus()Returns the exit status if this exception indicates that the application wasexited
. The exit status is intended to be passed toSystem.exit(int)
. In case of hard exit the application can be configured to callSystem.exit(int)
directly usingContext.Builder.useSystemExit(boolean)
.- Since:
- 19.0
- See Also:
-