Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/epl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2001 Alan Moore <alan_moore@gmx.net> Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de> Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr> Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se> Copyright (C) 2004 Thomas E Enebo <enebo@acm.org> Copyright (C) 2004 Joey Gibson <joey@joeygibson.com> Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de> Copyright (C) 2005 Charles O Nutter <headius@headius.com> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the EPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the EPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.exceptions;
 
 
 import org.jruby.Ruby;
 
 public class RaiseException extends JumpException {
     public static final boolean DEBUG = false;
     private static final long serialVersionUID = -7612079169559973951L;
     
     private RubyException exception;
     private String providedMessage;
     private boolean nativeException;

    
Construct a new RaiseException to wrap the given Ruby exception for Java-land throwing purposes. This constructor will generate a backtrace using the Java stack trace and the interpreted Ruby frames for the current thread.

Parameters:
actException The Ruby exception to wrap
 
     public RaiseException(RubyException actException) {
         this(actExceptionfalse);
     }

    
Construct a new RaiseException to wrap the given Ruby exception for Java-land throwing purposes. This constructor will not generate a backtrace and will instead use the one specified by the

Parameters:
exception The Ruby exception to wrap
backtrace
 
     public RaiseException(RubyException exceptionIRubyObject backtrace) {
         super(exception.message.toString());
         if () {
             Thread.dumpStack();
         }
         setException(exceptionfalse);
         preRaise(exception.getRuntime().getCurrentContext(), backtrace);
     }
 
     public RaiseException(Ruby runtimeRubyClass excptnClassString msgboolean nativeException) {
         super(msg);
         if (msg == null) {
             msg = "No message available";
         }
          = "(" + excptnClass.getName() + ") " + msg;
        this. = nativeException;
        if () {
            Thread.dumpStack();
        }
        setException((RubyException) Helpers.invoke(
                runtime.getCurrentContext(),
                excptnClass,
                "new",
                RubyString.newUnicodeString(excptnClass.getRuntime(), msg)),
                nativeException);
        preRaise(runtime.getCurrentContext());
    }
    public RaiseException(Ruby runtimeRubyClass excptnClassString msgIRubyObject backtraceboolean nativeException) {
        super(msg);
        if (msg == null) {
            msg = "No message available";
        }
         = "(" + excptnClass.getName() + ") " + msg;
        this. = nativeException;
        if () {
            Thread.dumpStack();
        }
        setException((RubyException) Helpers.invoke(
                runtime.getCurrentContext(),
                excptnClass,
                "new",
                RubyString.newUnicodeString(excptnClass.getRuntime(), msg)),
                nativeException);
        preRaise(runtime.getCurrentContext(), backtrace);
    }
    public RaiseException(RubyException exceptionboolean isNativeException) {
        super(exception.message.toString());
        if () {
            Thread.dumpStack();
        }
        this. = isNativeException;
        setException(exceptionisNativeException);
        preRaise(exception.getRuntime().getCurrentContext());
    }
    public RaiseException(Throwable causeNativeException nativeException) {
        super(buildMessage(cause), cause);
         = buildMessage(cause);
        setException(nativeExceptiontrue);
        preRaise(nativeException.getRuntime().getCurrentContext(), nativeException.getCause().getStackTrace());
    }

    
Method still in use by jruby-openssl <= 0.5.2
    public static RaiseException createNativeRaiseException(Ruby runtimeThrowable cause) {
        return createNativeRaiseException(runtimecausenull);
    }
    public static RaiseException createNativeRaiseException(Ruby runtimeThrowable causeMember target) {
        NativeException nativeException = new NativeException(runtimeruntime.getNativeException(), cause);
        // FIXME: someday, add back filtering of reflection/handle methods between JRuby and target
        return new RaiseException(causenativeException);
    }
    private static String buildMessage(Throwable exception) {
        StringBuilder sb = new StringBuilder();
        StringWriter stackTrace = new StringWriter();
        exception.printStackTrace(new PrintWriter(stackTrace));
    
        sb.append("Native Exception: '").append(exception.getClass()).append("'; ");
        sb.append("Message: ").append(exception.getMessage()).append("; ");
        sb.append("StackTrace: ").append(stackTrace.getBuffer().toString());
        return sb.toString();
    }
    @Override
    public String getMessage() {
        if ( == null) {
        }
        return ;
    }

    
Gets the exception

Returns:
Returns a RubyException
    public RubyException getException() {
        return ;
    }
    private void preRaise(ThreadContext context) {
        preRaise(context, (IRubyObject)null);
    }
    private void preRaise(ThreadContext contextStackTraceElement[] javaTrace) {
        context.runtime.incrementExceptionCount();
        doSetLastError(context);
        doCallEventHook(context);
        .prepareIntegratedBacktrace(contextjavaTrace);
        if (.) TraceType.dumpException();
    }
    private void preRaise(ThreadContext contextIRubyObject backtrace) {
        context.runtime.incrementExceptionCount();
        doSetLastError(context);
        doCallEventHook(context);
        
        if (backtrace == null) {
            .prepareBacktrace(context);
        } else {
            .forceBacktrace(backtrace);
        }
        // call Throwable.setStackTrace so that when RaiseException appears nested inside another exception,
        // Ruby stack trace gets displayed
        // JRUBY-2673: if wrapping a NativeException, use the actual Java exception's trace as our Java trace
        if ( instanceof NativeException) {
        } else {
        }
        if (.) TraceType.dumpException();
    }
    private void doCallEventHook(ThreadContext context) {
        if (context.runtime.hasEventHooks()) {
            context.runtime.callEventHooks(context.context.getFile(), context.getLine(), context.getFrameName(), context.getFrameKlazz());
        }
    }
    private void doSetLastError(ThreadContext context) {
        context.runtime.getGlobalVariables().set("$!");
    }
    
    
Sets the exception

Parameters:
newException The exception to set
    protected void setException(RubyException newExceptionboolean nativeException) {
        this. = newException;
        this. = nativeException;
    }
        StackTraceElement[] newTrace = new StackTraceElement[trace.length];
        for (int i = 0; i < newTrace.lengthi++) {
            newTrace[i] = trace[i].getElement();
        }
        return newTrace;
    }
New to GrepCode? Check out our FAQ X