Skip to content

Instantly share code, notes, and snippets.

@vvs
Created Mar 24, 2010
Embed
What would you like to do?
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