-1

我有一个内存不足的素数测试例程,我想找到它正在吐槽的值,但我无法打印出来!相关代码片段:

try{
            // truncate left to right
            for( int xTruncation = 1; xTruncation < lenValue; xTruncation++ ){
                integerValue[0]--;
                long value = Arithmetic.integerToLong10( integerValue );
                if( ! Numbers.primeIs( value ) ) continue NextPermutation; // the number is not prime
            }
            integerValue[0] = lenValue; // restore length

            // truncate right to left
            for( int xTruncation = 1; xTruncation < lenValue; xTruncation++ ){
                Arithmetic.integerReduce( integerValue, 1 );
                long value = Arithmetic.integerToLong10( integerValue );
                if( ! Numbers.primeIs( value ) ) continue NextPermutation; // the number is not prime
            }
} catch( Throwable t ) {
    System.out.println( "last value: " + nValue );
}

这是输出:

23
37
73
313
317
373
797
3137
3797
739397
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at cra.common.Numbers_jsc.factor(Numbers.java:153)
    at cra.common.Numbers_jsc.primeIs(Numbers.java:78)
    at Euler50.Euler_37(Euler50.java:803)
    at Euler50.main(Euler50.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

catch 子句中的 System.out.println 永远不会被调用。如何打印出有问题的值?

4

1 回答 1

0

您可以在System.out.println抛出异常之前添加。例如:

try{
            // truncate left to right
            for( int xTruncation = 1; xTruncation < lenValue; xTruncation++ ){
                integerValue[0]--;

                System.out.println( "integerValue: " + integerValue );
                long value = Arithmetic.integerToLong10( integerValue );
                System.out.println( "value: " + value );
                if( ! Numbers.primeIs( value ) ) continue NextPermutation; // the number is not prime
            }
            integerValue[0] = lenValue; // restore length

            // truncate right to left
            for( int xTruncation = 1; xTruncation < lenValue; xTruncation++ ){
                Arithmetic.integerReduce( integerValue, 1 );
                System.out.println( "integerValue: " + integerValue );
                long value = Arithmetic.integerToLong10( integerValue );
                System.out.println( "value: " + value );
                if( ! Numbers.primeIs( value ) ) continue NextPermutation; // the number is not prime
            }
} catch( Throwable t ) {
    System.out.println( "last value: " + nValue );
}
于 2013-06-21T16:46:48.857 回答