Skip to content

Commit

Permalink
Add doco about exception prep in constructors
Browse files Browse the repository at this point in the history
These methods that bottom out in `newRaiseException` or
`toThrowable` prepare the exception for an immediate throw, which
means:

* The backtrace will be populated
* The cause will be set to the currently in-flight exception
* The current thread's $! error info will be set to this exception

As such it should not be used to create a "simple" Ruby Exception
object for throwing on a different thread or at a later time.
  • Loading branch information
headius committed Nov 6, 2023
1 parent 01e585c commit bfe4d0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
15 changes: 14 additions & 1 deletion core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
Expand Up @@ -4209,6 +4209,16 @@ public RaiseException newSystemExit(int status, String message) {
return RubySystemExit.newInstance(this, status, message).toThrowable();
}

/**
* Prepare a throwable IOError with the given message.
*
* This constructor should not be used to create a RubyException object to be raised on a different thread.
*
* @see RaiseException#from(Ruby, RubyClass, String)
*
* @param message the message for the new IOError
* @return a fully-prepared IOError throwable
*/
public RaiseException newIOError(String message) {
return newRaiseException(getIOError(), message);
}
Expand Down Expand Up @@ -4312,10 +4322,13 @@ public RaiseException newBufferMaskError(String message) {
*
* There are additional forms of this construction logic in {@link RaiseException#from}.
*
* This constructor should not be used to create a RubyException object to be raised on a different thread.
*
* @see RaiseException#from(Ruby, RubyClass, String)
*
* @param exceptionClass the exception class from which to construct the exception object
* @param message a simple message for the exception
* @return a new RaiseException wrapping a new Ruby exception
* @see RaiseException#from(Ruby, RubyClass, String)
*/
public RaiseException newRaiseException(RubyClass exceptionClass, String message) {
IRubyObject cause = getCurrentContext().getErrorInfo();
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/org/jruby/exceptions/RaiseException.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public final Throwable fillInStackTrace() {
/**
* Construct a new throwable RaiseException appropriate for the target Ruby exception class.
*
* Note that this will produce a RaiseException that has fully prepared for being thrown, including:
*
* <ul>
* <li>Populating the stack trace based on the current thread</li>
* <li>Setting the cause to any currently in-flight exception</li>
* <li>Setting the thread-local $! "error info" to this exception</li>
* </ul>
*
* This constructor should not be used to create a RubyException object to be raised on a different thread.
*
* @param runtime the current JRuby runtime
* @param exceptionClass the class of the exception to construct and raise
* @param msg a simple message for the exception
Expand Down

0 comments on commit bfe4d0e

Please sign in to comment.