【问题标题】:My thrown exception running into StackOverflowError我抛出的异常遇到 StackOverflowError
【发布时间】:2016-05-21 03:44:54
【问题描述】:

我有以下简单的递归斐波那契代码:

public class FibPrac5202016
{
public static void main(String [] args)  {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter index number: ");
    int integer = input.nextInt();
  FibPrac5202016 object = new FibPrac5202016();

System.out.println(object.operation(integer));
}

public static long operation(long n) {
 if(n==0)
     return 0;
 if(n==1)
     return 1;
 try {
     if( n < 0)
     throw new Exception("Positive Number Required");

 }
 catch(Exception exc)
 {
     System.out.println("Error: " + exc.getMessage());

 }

 return operation((n-1))+operation((n-2));
}

}

正如我最近了解的异常一样,当用户输入负整数时,我尝试在这里使用它。但是,我的程序遇到了 StackOverflowError。

【问题讨论】:

    标签: java exception stack-overflow


    【解决方案1】:

    是的,因为你递归之后你捕捉到Exception。您可以通过在 catch 中返回 -1 来轻松修复它。

    catch(Exception exc)
    {
        System.out.println("Error: " + exc.getMessage());
        return -1;
    }
    

    一开始就不要抛出Exception,比如

    public static long operation(long n) {
       if (n < 0) { 
           return -1;
       } else if (n == 0) {
           return 0;
       } else if (n == 1 || n == 2) {
           return 1;
       }
       return operation((n-1))+operation((n-2));
    }
    

    您可以实现Negafibonaccis。而且,您可以扩展它以支持 BigInteger(并使用 memoization 进行优化),例如

    private static Map<Long, BigInteger> memo = new HashMap<>();
    static {
        memo.put(0L, BigInteger.ZERO);
        memo.put(1L, BigInteger.ONE);
        memo.put(2L, BigInteger.ONE);
    }
    
    public static BigInteger operation(long n) {
        if (memo.containsKey(n)) {
            return memo.get(n);
        }
        final long m = Math.abs(n);
        BigInteger ret = n < 0 //
                ? BigInteger.valueOf(m % 2 == 0 ? -1 : 1).multiply(operation(m))
                : operation((n - 2)).add(operation((n - 1)));
        memo.put(n, ret);
        return ret;
    }
    

    【讨论】:

      【解决方案2】:

      问题是这些会在 try 块中抛出一个 execcion,这会创建一个循环,在该循环中测试代码,并且总是小于 0 的数字总是无限抛出异常,直到给出异常

      线程“主”java.lang.StackOverflowError 中的异常

      我认为解决办法是当你发现一个小于 0 的数字时让程序停止

      如下

      public class FibPrac5202016 {
      public static void main(String [] args)  {
          Scanner input = new Scanner(System.in);
          System.out.println("Enter index number: ");
          int integer = input.nextInt();
          FibPrac5202016 object = new FibPrac5202016();
      
              System.out.println(object.operation(integer));
       }
      
       public static long operation(long n) {
        if(n==0)
          return 0;
        if(n==1)
          return 1;
        try 
      {
          if( n < 0)            
              throw new Exception("Positive Number Required");   
      }
      catch(Exception exc)
      {
          System.out.println("Error: " + exc.getMessage());
          //return -1;
      }
      
      return operation((n-1))+operation((n-2));
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2023-04-03
        • 1970-01-01
        • 2018-10-18
        • 2013-05-24
        • 2015-10-28
        • 2012-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多