2016-07-04 5 views
2

スタックスペースを使用した再帰とその欠点を示すためです。私は以下のコードを書いた。 Nが非常に大きいとき(100000のような)、予想されるエラー( "java.lang.StackOverflowError")を返すことがわかった。次に、以下のクラスとそれに続くドライバクラスを使用して、この特定のエラーをキャッチしようとしました。以下の結果に示すように、しかし、NetBeans IDEには「ヌル」を返します。「スタックオーバーフロー」エラーをキャッチすると「Null」が返されます。

Caught stack Overflow error: null

The factorial of log of 100000 is 68687.75095683799

Direct calculation 1051299.221899134

BUILD SUCCESSFUL (total time: 0 seconds)

は、実際のエラーを返す方法はありますか?私が間違ってやっていることについて誰か助けてくれましたか?

package recursiondemo; 

import static java.lang.Math.log; 

/** This class demonstrates the recursion with calculation of the value log(N!) 
* log(N!) = log(N*(N-1).....3*2*1) = log(N) + log (N-1) + ......log(3) + log (2) + log(1) 
* @author = 
*/ 
public class logRecursion implements recursionInterface { 
    //private int localCounter = 0; 

    public logRecursion(){ 

    } 

    /** 
    * 
    * @param localCounter 
    * @return 
    */ 
    //@Override 
    public double directCalculation(int localCounter){ 
     double result = 0.0; 
     int loopCounter = localCounter; 

     while (loopCounter >=1) { 
      result += log(loopCounter); 
      --loopCounter; 
     } 
     return result; 
    } 

    public double calculation(int localCounter) throws Exception{ 

     if (localCounter == 1) { 
      return 0.0; 
     } 

     if (localCounter <= 0) { 
      throw new Exception("Factorials are not defined for the input given"); 
     } 
     try { 
      return log(localCounter) + calculation(localCounter - 1); // Recursion 
     } 
     catch (StackOverflowError e) { 
     System.err.println("Caught stack Overflow error: " + e.getMessage()); 
     } 
     return 0.0; // This is an arbitrary return value to avoid compile time error of no return parameter. So this return value is meaning less 
    } 

} 

package recursiondemo; 

/** 
* Driver class 
* @author 
*/ 
public class RecursionDemo { 

    /** 
    * @param args the command line arguments 
    * @throws java.lang.Exception 
    */ 
    public static void main(String[] args) throws Exception { 
     // TODO code application logic here 
     logRecursion test; 
     test = new logRecursion(); 
     System.out.println("The factorial of log of " + args[0] + " is " + test.calculation(Integer.parseInt(args[0]))); // Recursion 
     System.out.println("Direct calculation " + test.directCalculation(Integer.parseInt(args[0]))); // Direct calculation 
    } 

} 
+1

StakOverflowErrorにメッセージがあるかどうかは不明です。さらに、catch節のprintlnはおそらく別のStackOverflowErrorを引き起こします。 – Henry

+0

@Henryなぜですか? catchブロックが実行されている場合、tryブロックが完了したこと、関数が完了したこと(成功したかどうか)、スタックから外れたことを意味します。 –

+0

@ Henry、スタックスペースが吹き飛ばされているので意味をなさない。 – ComputationalPhysicist

答えて

2

あなたStackOverflowError(多くの場合、ランタイム環境によってスローされた例外の場合である)のメッセージを持っていません。そのため、あなたのログステートメントはnullと印刷されます。これは通常は捕捉したい例外/エラーではないことに注意してください。とにかく

、あなたは(1がある場合、+詳細メッセージ)ちょうどそのクラス名を返します。エラーのtoString()メソッドを使用し、コンソールに、より意味のあるメッセージをログに記録する場合:

System.err.println("Caught stack Overflow error: " + e); 
+0

ありがとうございます。それを試し、予想される例外を返しました。 – ComputationalPhysicist

関連する問題