Attached is a patch to DefaultMethod that attempts to wrap all RuntimeException instances passing through it with a Ruby NativeException, allowing you to catch them as normal. Consider it an experimental start.

There are caveats:

- Since the script you specify on the command line immediately compiles, this won't work on that script (it only patches interpreted/jitted call paths). Specify -X-C or require the file in. - This only catches RuntimeException. We'd need to generate code to catch and rethrow non-RuntimeException because of Java's checked exception "throws" requirement. - This will slow down propagation of all RuntimeException instances, including normal RaiseExceptions from Ruby. There's no way around this with RaiseException descending from RuntimeException.

- Charlie
diff --git a/src/org/jruby/internal/runtime/methods/DefaultMethod.java 
b/src/org/jruby/internal/runtime/methods/DefaultMethod.java
index ee83140..fc0b111 100644
--- a/src/org/jruby/internal/runtime/methods/DefaultMethod.java
+++ b/src/org/jruby/internal/runtime/methods/DefaultMethod.java
@@ -141,6 +141,10 @@ public final class DefaultMethod extends DynamicMethod 
implements JumpTarget {
                 return handleReturn(rj);
             } catch (JumpException.RedoJump rj) {
                 return handleRedo(runtime);
+            } catch (RaiseException raise) {
+                throw raise;
+            } catch (RuntimeException re) {
+                throw RaiseException.createNativeRaiseException(runtime, re);
             } finally {
                 jitPost(runtime, context, name);
             }
@@ -174,6 +178,10 @@ public final class DefaultMethod extends DynamicMethod 
implements JumpTarget {
             return handleReturn(rj);
         } catch (JumpException.RedoJump rj) {
             return handleRedo(runtime);
+        } catch (RaiseException raise) {
+            throw raise;
+        } catch (RuntimeException re) {
+            throw RaiseException.createNativeRaiseException(runtime, re);
         } finally {
             if (runtime.hasEventHooks()) {
                 traceReturn(context, runtime, name);
@@ -195,6 +203,10 @@ public final class DefaultMethod extends DynamicMethod 
implements JumpTarget {
                 return handleReturn(rj);
             } catch (JumpException.RedoJump rj) {
                 return handleRedo(runtime);
+            } catch (RaiseException raise) {
+                throw raise;
+            } catch (RuntimeException re) {
+                throw RaiseException.createNativeRaiseException(runtime, re);
             } finally {
                 jitPost(runtime,context, name);
             }
@@ -216,6 +228,10 @@ public final class DefaultMethod extends DynamicMethod 
implements JumpTarget {
                 return handleReturn(rj);
             } catch (JumpException.RedoJump rj) {
                 return handleRedo(runtime);
+            } catch (RaiseException raise) {
+                throw raise;
+            } catch (RuntimeException re) {
+                throw RaiseException.createNativeRaiseException(runtime, re);
             } finally {
                 jitPost(runtime, context, name);
             }
@@ -237,6 +253,10 @@ public final class DefaultMethod extends DynamicMethod 
implements JumpTarget {
                 return handleReturn(rj);
             } catch (JumpException.RedoJump rj) {
                 return handleRedo(runtime);
+            } catch (RaiseException raise) {
+                throw raise;
+            } catch (RuntimeException re) {
+                throw RaiseException.createNativeRaiseException(runtime, re);
             } finally {
                 jitPost(runtime, context, name);
             }
@@ -258,6 +278,10 @@ public final class DefaultMethod extends DynamicMethod 
implements JumpTarget {
                 return handleReturn(rj);
             } catch (JumpException.RedoJump rj) {
                 return handleRedo(runtime);
+            } catch (RaiseException raise) {
+                throw raise;
+            } catch (RuntimeException re) {
+                throw RaiseException.createNativeRaiseException(runtime, re);
             } finally {
                 jitPost(runtime, context, name);
             }
@@ -279,6 +303,10 @@ public final class DefaultMethod extends DynamicMethod 
implements JumpTarget {
                 return handleReturn(rj);
             } catch (JumpException.RedoJump rj) {
                 return handleRedo(runtime);
+            } catch (RaiseException raise) {
+                throw raise;
+            } catch (RuntimeException re) {
+                throw RaiseException.createNativeRaiseException(runtime, re);
             } finally {
                 jitPost(runtime, context, name);
             }
@@ -300,6 +328,10 @@ public final class DefaultMethod extends DynamicMethod 
implements JumpTarget {
                 return handleReturn(rj);
             } catch (JumpException.RedoJump rj) {
                 return handleRedo(runtime);
+            } catch (RaiseException raise) {
+                throw raise;
+            } catch (RuntimeException re) {
+                throw RaiseException.createNativeRaiseException(runtime, re);
             } finally {
                 jitPost(runtime, context, name);
             }
@@ -321,6 +353,10 @@ public final class DefaultMethod extends DynamicMethod 
implements JumpTarget {
                 return handleReturn(rj);
             } catch (JumpException.RedoJump rj) {
                 return handleRedo(runtime);
+            } catch (RaiseException raise) {
+                throw raise;
+            } catch (RuntimeException re) {
+                throw RaiseException.createNativeRaiseException(runtime, re);
             } finally {
                 jitPost(runtime, context, name);
             }
@@ -342,6 +378,10 @@ public final class DefaultMethod extends DynamicMethod 
implements JumpTarget {
                 return handleReturn(rj);
             } catch (JumpException.RedoJump rj) {
                 return handleRedo(runtime);
+            } catch (RaiseException raise) {
+                throw raise;
+            } catch (RuntimeException re) {
+                throw RaiseException.createNativeRaiseException(runtime, re);
             } finally {
                 jitPost(runtime, context, name);
             }
@@ -363,6 +403,10 @@ public final class DefaultMethod extends DynamicMethod 
implements JumpTarget {
                 return handleReturn(rj);
             } catch (JumpException.RedoJump rj) {
                 return handleRedo(runtime);
+            } catch (RaiseException raise) {
+                throw raise;
+            } catch (RuntimeException re) {
+                throw RaiseException.createNativeRaiseException(runtime, re);
             } finally {
                 jitPost(runtime, context, name);
             }

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Reply via email to