我抛出的异常 运行 进入 StackOverflowError
My thrown exception running into StackOverflowError
我有以下简单的递归斐波那契代码:
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));
}
}
正如我最近了解到的异常,当用户输入负数时,我试图在此处使用它 integer.However,我的程序运行到 WhosebugError。
嗯,是的,因为你在 之后递归 ,你会得到一个 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. And, you could extend it to support BigInteger
(and optimize with 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;
}
问题是这些在 try 块中抛出一个 execcion,这会创建一个循环,在这个循环中测试代码,并且总是小于 0 的数字总是无限地抛出异常,直到给出异常
Exception in thread "main" java.lang.WhosebugError
我认为解决方案是当你发现一个小于 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));
}
}
我有以下简单的递归斐波那契代码:
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));
}
}
正如我最近了解到的异常,当用户输入负数时,我试图在此处使用它 integer.However,我的程序运行到 WhosebugError。
嗯,是的,因为你在 之后递归 ,你会得到一个 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. And, you could extend it to supportBigInteger
(and optimize with 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;
}
问题是这些在 try 块中抛出一个 execcion,这会创建一个循环,在这个循环中测试代码,并且总是小于 0 的数字总是无限地抛出异常,直到给出异常
Exception in thread "main" java.lang.WhosebugError
我认为解决方案是当你发现一个小于 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));
}
}