Skip to content

Instantly share code, notes, and snippets.

@vvs
Created March 24, 2010 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvs/342141 to your computer and use it in GitHub Desktop.
Save vvs/342141 to your computer and use it in GitHub Desktop.
require 'java'
$CLASSPATH << "."
def foo
foo
end
begin
# These all work as expected:
#
# java.lang.Integer.decode('aaa')
# Java::Thrower::throwError
# Java::Thrower::throwOutOfMemoryError
# Java::Thrower::throwStackOverflow
# Java::Thrower::throwNPE
# These don't work with rescue Exception:
#
# foo # this causes stack overflow and exits JRuby:
# Error: Your application used more stack memory than the safety cap of 1024k.
# Can be rescued with rescue java.lang.StackOverflowError though.
#
#
# raise java.lang.NullPointerException.new("baaaa") # exits JRuby:
# NativeConstructorAccessorImpl.java:-2:in `newInstance0': java.lang.NullPointerException: baaaa
# from NativeConstructorAccessorImpl.java:39:in `newInstance'
# from DelegatingConstructorAccessorImpl.java:27:in `newInstance'
# from Constructor.java:513:in `newInstance'
#
#
# a = ''; 1000000.times {a << String.new('a'*10000)} # produces OOME
# Error: Your application used more memory than the safety cap of 500m.
rescue Exception => e
puts "Caught in Ruby: #{e.class} -- #{e}"
end
public class Thrower {
public static void throwError() {
System.err.println("About to throw Error");
throw new UnknownError("Unknown Error");
}
public static void throwOutOfMemoryError () {
System.err.println("About to throw OutOfMemoryError ");
throw new OutOfMemoryError ("No Memory Error");
}
public static void throwNPE() {
System.err.println("About to throw NPE");
throw new NullPointerException("NPE");
}
public static void throwStackOverflow() {
throwStackOverflow();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment