Java 抛出内存不足错误后,需要打印Catch语句中的值
我有一个素性测试例程,内存不足,我想找到它的值,但我无法打印出来!相关代码片段:Java 抛出内存不足错误后,需要打印Catch语句中的值,java,exception,memory,Java,Exception,Memory,我有一个素性测试例程,内存不足,我想找到它的值,但我无法打印出来!相关代码片段: try{ // truncate left to right for( int xTruncation = 1; xTruncation < lenValue; xTruncation++ ){ integerValue[0]--; long value = Arithmetic.integerTo
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 );
}
catch子句中的System.out.println从未被调用如何打印出有问题的值?您可以在引发异常之前添加
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 );
}
试试看{
//从左到右截断
对于(int-xtrun阳离子=1;xtrun阳离子
您的循环似乎可以组合,并且在继续之后,您也不会显示标签将您带到哪里。异常处理程序代码究竟如何运行?根据定义,您已经耗尽了内存,因此没有更多的内存来执行处理程序所需的任何分配(生成异常字符串、回溯等的临时变量)。异常抛出的确切位置是哪里?是不是Numbers.java:153可能就是这条线?(我认为这几乎是唯一可能发生这种情况的方式。)这不是重复,我知道如何捕获错误,问题是捕获错误后如何打印。基本问题是,在捕获异常时,没有足够的空间来执行任何操作。有时,您可以通过预先分配大量存储(多个小型/中型对象与一个大型对象),然后在尝试打印之前将对该存储的引用归零来避免这种情况。
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 );
}